.net操作Word:我做了一个Word模板,我想实现这样的功能,在数据库去数据然后,替换word摸班的内容,保存为临时文件,在IE中打开????怎么做呀,。我找了一个原先的帖子,有是有关于Word的操作,但是没有这里的替换功能,怎么做呀

解决方案 »

  1.   

    使用vbscript,我这里有个例子,你参考一下,我是在页面上直接生成的word。你换成打开现有word文档就可以了。如果再不清楚可以上msdn看一下,里面讲的内容比较多。
    Dim myWord,ActDoc,CurRange,CurTable
    Sub PrintChange()

    ' 取页面控件值
    Dim FieldValue()
    Dim CurCell,tbWidth,FieldCount

    Set myWord = createobject("word.application")
    wordVer=myWord.version
    myWord.Documents.Add
    myWord.DisplayAlerts = 0'wdAlertsNone

    Set ActDoc = myWord.ActiveDocument
    ' 边框
    With ActDoc.PageSetup
    .LineNumbering.Active = False
    .Orientation = 1'wdOrientLandscape
    .TopMargin = myWord.CentimetersToPoints(2.2)
    .BottomMargin = myWord.CentimetersToPoints(1)
    .LeftMargin = myWord.CentimetersToPoints(2)
    .RightMargin = myWord.CentimetersToPoints(2)
    End With

    With ActDoc.PageSetup.TextColumns
    .SetCount(2)
    .EvenlySpaced = True
    .LineBetween = False
    .Width = myWord.CentimetersToPoints(7)
    .Spacing = myWord.CentimetersToPoints(3.5)
    End With
          
    Set CurRange = ActDoc.Range(0, 0)
        
    With CurRange
    .Text = "变更事项"
    .Paragraphs.Alignment = 1'wdAlignParagraphCenter
    .Bold = True
    .Font.Size = 16
    End With
    CurRange.Collapse 0
        
    DrawTable
    CurRange.InsertParagraphAfter
    CurRange.Collapse 0
        
    DrawTable
    CurRange.InsertParagraphAfter
    CurRange.Collapse 0
        
    DrawTable
    CurRange.InsertParagraphAfter
    CurRange.Collapse 0
        
    DrawTable
    CurRange.Collapse 0
    CurRange.InsertParagraphAfter

    myWord.visible=true 

    ActDoc.FormFields.Shaded = True

    myWord.activedocument.saved = True  End Sub Sub DrawTable()

    Set CurTable = ActDoc.Tables.Add(CurRange, 4, 1) With CurTable.Rows(1).Range
    .Text = "经审定,"
    .Paragraphs.Alignment = 0 'wdAlignParagraphLeft
    End With Set CurRange = CurTable.Rows(2).Range
    CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput
    CurTable.Rows(2).Height = myWord.CentimetersToPoints(2) With CurTable.Rows(3).Range
    .Text = "同意变更。" + "    "
    .Paragraphs.Alignment = 2 'wdAlignParagraphRight
    End With '先把整行分为两列
    CurTable.Cell(4, 1).Split 1, 2
    '再拆分第1列为两列处理审批人信息
    CurTable.Cell(4, 1).Split 1, 2 With CurTable.Cell(4, 1).Range
    .Text = Space(5) + "审批人"
    .Paragraphs.Alignment = 0 'wdAlignParagraphLeft
    End With
    ' 域,审批人
    Set CurRange = CurTable.Cell(4,2).Range
    CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput CurTable.Cell(4, 3).Split 1, 6 CurTable.Cell(4, 4).Range.Text = "年"
    CurTable.Cell(4, 6).Range.Text = "月"
    CurTable.Cell(4, 8).Range.Text = "日"

    For i=1 To 3
    Set CurRange = CurTable.Cell(4,2*i+1).Range
    CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput
    Next 

    Set CurRange = CurTable.Range
    CurRange.Collapse 0 End Sub
      

  2.   

    /// <summary>
    /// 向Word写入数据
    /// </summary>
    private void ToWord(string[] pStr_XTITLE,string[] pStr_CCONTENT)
    {
    object missing = System.Reflection.Missing.Value;
    object readOnly = false;
    object isVisible = true;
    object oTemplate="C:\\Inetpub\\wwwroot\\Evaluation\\file\\word.dot";
    object file="c:\\myfile4.doc";
    ApplicationClass oWordApp = new ApplicationClass();
    oWordApp.Visible=true;
    // this.Response.Write(this.XTITLE.Text); Document oWordDoc = oWordApp.Documents.Open(ref oTemplate,  ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing,ref missing,ref missing);
    for (int i=0;i<pStr_XTITLE.Length;i++)
    {
    for (int j = 1; j <= oWordDoc.Books.Count; j++) 

    object objI = j; 
    if(oWordDoc.Books.Item(ref objI).Name==pStr_XTITLE[i].Trim()) 
    oWordDoc.Books.Item(ref objI).Range.Text =pStr_CCONTENT[i].Trim();  
    } /*object oBookMark =pStr_XTITLE[i].Trim();
    oWordDoc.Books.Item(ref oBookMark).Range.Text =pStr_CCONTENT[i].Trim(); 
    this.Response.Write(pStr_XTITLE[i]);*/
    }
    oWordDoc.SaveAs(ref file,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); oWordApp.Application.Quit(ref missing, ref missing, ref missing);
    oWordApp=null;
    }不知是不是你所要的