http://www.c-sharpcorner.com/Code/2002/Mar/WordFromDotNet.asp

解决方案 »

  1.   

    //创建
    TitleQuery theQueryDialog = new TitleQuery();
    if (theQueryDialog.ShowDialog() == DialogResult.OK)
    {
    // vba code to remind me how to do it.
    // Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    //    Selection.Font.Bold = wdToggle
    //    Selection.TypeText Text:="Creating a Title"
    //    Documents.Add Template:="C:\My Documents\CSharp Book Project\Normal.dot", _
    //      NewTemplate:=False, DocumentType:=0
    object missing = System.Reflection.Missing.Value;
    object fileName = "normal.dot";
    object newTemplate = false;
    object docType = 0;
    object isVisible = true;
    Word.Document aDoc = WordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
    WordApp.Visible = true;
    aDoc.Activate();
    WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    WordApp.Selection.Font.Bold = (int)Word.WdConstants.wdToggle;
    WordApp.Selection.TypeText(theQueryDialog.Title);
    //打开
    if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    object fileName = openFileDialog1.FileName;
    object readOnly = false;
    object isVisible = true;
    object missing = System.Reflection.Missing.Value; // WordApp.Activate();
    WordApp.Visible = true; Word.Document aDoc = WordApp.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); aDoc.Activate(); WordApp.Selection.TypeText("Copyright C# Corner");
    WordApp.Selection.TypeParagraph(); }
      

  2.   

    word.Application word= new word.Application();
      int rowIndex=1;
      int colIndex=0;  word.Application.Workbooks.Add(true);
        
      DataTable table=GetData();
         
      //将所得到的表的列名,赋值给单元格
      foreach(DataColumn col in table.Columns)
      {
       colIndex++; 
       word.Cells[1,colIndex]=col.ColumnName;    
      }  //同样方法处理数据
      foreach(DataRow row in table.Rows)
     {
        rowIndex++;
        colIndex=0;
        foreach(DataColumn col in table.Columns)
        {
     colIndex++;
     word.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
        }
      }
      //不可见,即后台处理
      word.Visible=true;  
    }