我现在是在做一个合同书。合同书要按版面打印,我想在word里面设计好版面,然后从数据库里边提取相应的数据添在word里边去。。 
比如在word里已经设计好如下版面:
           项目名称:____________________________
           申请单位:____________________________
           协作单位:____________________________
                  。
                  。
                  。 
我要把数据库里的projectName放到项目名称后边显示,applyUint放到申请单位后边显示。那该怎么做呢?我其实最终目的是为了打印这个文件。用web页打印可是版面不好控制,我就想用word做。各位大牛有什么好办法嘛?不用word也可以。。多谢了,后天就要去交差了。可是我还没头绪咧

解决方案 »

  1.   

    在word里需要填入数据的部分加标签,然后用程序控制就可以了
      

  2.   

    给你一段以前写的导出到word模版的代码:
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Data;
    using Microsoft.Office.Interop.Word;namespace response
    {
    /// <summary>
    /// ToWordDoc 的摘要说明。
    /// </summary>
    public class ToWordDoc
    {
    private ApplicationClass oWordApplic; // a reference to Word application
    private Document oDoc; // a reference to the document
    public ToWordDoc()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    // activate the interface with the COM object of Microsoft Word
    oWordApplic = new ApplicationClass();
    } // Open a file (the file must exists) and activate it
    public void Open(string strFileName)
    {
    object fileName = strFileName;
    object readOnly = false;
    object isVisible = true;
    object missing = System.Reflection.Missing.Value; oDoc = oWordApplic.Documents.Open(ref fileName, ref missing,ref readOnly, 
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing,ref missing); oDoc.Activate();
    }
    // Open a new document
    public void Open( )
    {
    object missing = System.Reflection.Missing.Value;
    oDoc = oWordApplic.Application.Documents.Add(ref missing, ref missing,ref missing, ref missing); oDoc.Activate();
    }
    public void Quit( )
    {
    // object missing = System.Reflection.Missing.Value;
    // oWordApplic.Application.Quit(ref missing, ref missing, ref missing);

    object missing = System.Reflection.Missing.Value;
    oDoc.Close(ref missing, ref missing, ref missing);
    oWordApplic.Quit(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic); oDoc = null;
    oWordApplic = null;
    GC.Collect();
    } public void Save( )
    {
    oDoc.Save();
    } public void SaveAs(string strFileName)
    {
    object missing = System.Reflection.Missing.Value;
    object fileName = strFileName;
    //object Format = (int)WdSaveFormat.wdFormatFilteredHTML;
    oDoc.SaveAs(ref fileName, ref missing,ref missing, ref missing,ref missing,ref missing,ref missing,
    ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } // Save the document in HTML format
    public void SaveAsHtml(string strFileName )
    {
    object missing = System.Reflection.Missing.Value;
    object fileName = strFileName;
    object Format = (int)WdSaveFormat.wdFormatHTML;
    oDoc.SaveAs(ref fileName, ref Format,ref missing, ref missing,ref missing,ref missing,ref missing,
    ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    } public void InsertText( string strText)
    {
    oWordApplic.Selection.TypeText(strText);
    } public string InsertHtml(string strText)
    {
    string noHtml = "";//System.Web.HttpUtility.HtmlDecode(strText);//
    noHtml =  strText.Replace("<p>","").Replace("<P>","").Replace("</P>","").Replace("</p>","");
    noHtml = noHtml.Replace("&nbsp;"," ").Replace("<br>","\n").Replace("<BR>","\n"); return noHtml;
    } public void InsertLineBreak( )
    {
    oWordApplic.Selection.TypeParagraph();
    }
    public void InsertLineBreak( int nline)
    {
    for (int i=0; i<nline; i++)
    oWordApplic.Selection.TypeParagraph();
    } // Change the paragraph alignement
    public void SetAlignment(string strType )
    {
    switch (strType)
    {
    case "Center" :
    oWordApplic.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    break;
    case "Left" :
    oWordApplic.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
    break;
    case "Right" :
    oWordApplic.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
    break;
    case "Justify" :
    oWordApplic.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
    break;
    }

    }
    // if you use thif function to change the font you should call it again with 
    // no parameter in order to set the font without a particular format
    public void SetFont( string strType )
    {
    switch (strType)
    {
    case "Bold":
    oWordApplic.Selection.Font.Bold = 1;
    break;
    case "Italic":
    oWordApplic.Selection.Font.Italic = 1;
    break;
    case "Underlined":
    oWordApplic.Selection.Font.Subscript = 0;
    break;
    }

    }
      

  3.   

    //续上
    // disable all the style 
    public void SetFont( )
    {
    oWordApplic.Selection.Font.Bold = 0;
    oWordApplic.Selection.Font.Italic = 0;
    oWordApplic.Selection.Font.Subscript = 0;

    } public void SetFontName( string strType )
    {
    oWordApplic.Selection.Font.Name = strType;

    }  public void SetFontSize( int nSize )
    {
    oWordApplic.Selection.Font.Size = nSize;

    }  public void InsertPagebreak()
    {
    // VB : Selection.InsertBreak Type:=wdPageBreak
    object pBreak= (int)WdBreakType.wdPageBreak;
    oWordApplic.Selection.InsertBreak(ref pBreak );
    } // Go to a predefined book, if the book doesn't exists the application will raise an error public void GotoBookMark( string strBookMarkName)
    {
    // VB :  Selection.GoTo What:=wdGoToBook, Name:="nome"
    object missing = System.Reflection.Missing.Value; object Book = (int)WdGoToItem.wdGoToBook;
    object NameBookMark = strBookMarkName;
    oWordApplic.Selection.GoTo(ref Book, ref missing, ref missing,ref NameBookMark);
    } public void GoToTheEnd( )
    {
    // VB :  Selection.EndKey Unit:=wdStory
    object missing = System.Reflection.Missing.Value;
    object unit ;
    unit = WdUnits.wdStory ;
    oWordApplic.Selection.EndKey ( ref unit, ref missing);


    public void GoToTheBeginning( )
    {
    // VB : Selection.HomeKey Unit:=wdStory
    object missing = System.Reflection.Missing.Value;
    object unit ;
    unit = WdUnits.wdStory ;
    oWordApplic.Selection.HomeKey ( ref unit, ref missing);

    }  public void GoToTheTable(int ntable )
    {
    // Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
    //    Selection.Find.ClearFormatting
    //    With Selection.Find
    //        .Text = ""
    //        .Replacement.Text = ""
    //        .Forward = True
    //        .Wrap = wdFindContinue
    //        .Format = False
    //        .MatchCase = False
    //        .MatchWholeWord = False
    //        .MatchWildcards = False
    //        .MatchSoundsLike = False
    //        .MatchAllWordForms = False
    //    End With object missing = System.Reflection.Missing.Value;
    object what;
    what = WdUnits.wdTable ;
    object which;
    which = WdGoToDirection.wdGoToFirst;
    object count;
    count = 1 ;
    oWordApplic.Selection.GoTo( ref what, ref which, ref count, ref missing);
    oWordApplic.Selection.Find.ClearFormatting(); oWordApplic.Selection.Text = "";


    }  public void GoToRightCell( )
    {
    // Selection.MoveRight Unit:=wdCell

    object missing = System.Reflection.Missing.Value;
    object direction;
    direction = WdUnits.wdCell;
    oWordApplic.Selection.MoveRight(ref direction,ref missing,ref missing);
    }  public void GoToLeftCell( )
    {
    // Selection.MoveRight Unit:=wdCell

    object missing = System.Reflection.Missing.Value;
    object direction;
    direction = WdUnits.wdCell;
    oWordApplic.Selection.MoveLeft(ref direction,ref missing,ref missing);
    }  public void GoToDownCell( )
    {
    // Selection.MoveRight Unit:=wdCell

    object missing = System.Reflection.Missing.Value;
    object direction;
    direction = WdUnits.wdLine;
    oWordApplic.Selection.MoveDown(ref direction,ref missing,ref missing);
    }  public void GoToUpCell( )
    {
    // Selection.MoveRight Unit:=wdCell

    object missing = System.Reflection.Missing.Value;
    object direction;
    direction = WdUnits.wdLine;
    oWordApplic.Selection.MoveUp(ref direction,ref missing,ref missing);

    // this function doesn't work
    public void InsertPageNumber( string strType, bool bHeader )
    {
    object missing = System.Reflection.Missing.Value;
    object alignment ;
    object bFirstPage = false;
    object bF = true;
    //if (bHeader == true)
    //WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber = bF;
    switch (strType)
    {
    case "Center":
    alignment = WdPageNumberAlignment.wdAlignPageNumberCenter;
    //WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage);
    //Selection objSelection = WordApplic.pSelection;

    oWordApplic.Selection.HeaderFooter.PageNumbers[1].Alignment = WdPageNumberAlignment.wdAlignPageNumberCenter;
    break;
    case "Right":
    alignment = WdPageNumberAlignment.wdAlignPageNumberRight;
    oWordApplic.Selection.HeaderFooter.PageNumbers[1].Alignment = WdPageNumberAlignment.wdAlignPageNumberRight;
    break;
    case "Left":
    alignment = WdPageNumberAlignment.wdAlignPageNumberLeft;
    oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage);
    break;
    }
                
    }
    }
    //object units = WdUnits.wdCharacter;
    //object last=doc.Characters.Count;
    //doc.Range(ref first, ref last).Delete(ref units, ref last)}
      

  4.   

    VBA替换标签,这个有时候真让人烦啊
      

  5.   

    楼上的vba替换是什么东西啊?能给详细说说吗?
      

  6.   

    谢谢timeryzh(保持距离),不过还是解决不了我的问题啊,只要能在word里边定位就好了。。在相应的位置导入相应的数据库数据就ok。
      

  7.   

    http://blog.csdn.net/goody9807/articles/145876.aspx
      

  8.   

    在word的模板文件里插入书签,书签的名字就是你要find的!
      

  9.   

    1,我的水晶报表在运行中总是偏到页面的右边,无论我怎么放reportview的位置。什么原因?  
    2,报表中有一个序号列(number),但我的数据库表中没有该字段。怎么才能在生成报表的时候动态生成该序号列??  
     
     
     
    不胜感谢!!!  
      

  10.   

    楼上的的什么master阿?能具体说一下吗?
      

  11.   

    谢谢楼上的.
    [email protected]
    多谢!!!!!!!