2016-04-29 37 views
0

我有這個代碼從數據網格視圖導出到word文檔,但我需要導出到現有的word文件不創建另一個? 因爲我有一個word文檔與兩個圖像的漂亮的頭球,我需要創建它沒有創造一個又一個datagridview到現有的word文件中的C#word

using Word = Microsoft.Office.Interop.Word; 

    public void Export_Data_To_Word(DataGridView DGV, string filename) 
    { 
    if (DGV.Rows.Count != 0) 
    { 
     int RowCount = DGV.Rows.Count; 
     int ColumnCount = DGV.Columns.Count; 
     Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1]; 

     //add rows 
     int r = 0; 
     for (int c = 0; c <= ColumnCount - 1; c++) 
     { 
      for (r = 0; r <= RowCount - 1; r++) 
      { 
       DataArray[r, c] = DGV.Rows[r].Cells[c].Value; 
      } //end row loop 
     } //end column loop 

     Word.Document oDoc = new Word.Document(); 
     oDoc.Application.Visible = true; 

     //page orintation 
     oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; 


     dynamic oRange = oDoc.Content.Application.Selection.Range; 
     string oTemp = ""; 
     for (r = 0; r <= RowCount - 1; r++) 
     { 
      for (int c = 0; c <= ColumnCount - 1; c++) 
      { 
       oTemp = oTemp + DataArray[r, c] + "\t"; 

      } 
     } 

     //table format 
     oRange.Text = oTemp; 

     object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs; 
     object ApplyBorders = true; 
     object AutoFit = true; 
     object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent; 

     oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount, 
           Type.Missing, Type.Missing, ref ApplyBorders, 
           Type.Missing, Type.Missing, Type.Missing, 
           Type.Missing, Type.Missing, Type.Missing, 
           Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing); 

     oRange.Select(); 

     oDoc.Application.Selection.Tables[1].Select(); 
     oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0; 
     oDoc.Application.Selection.Tables[1].Rows.Alignment = 0; 
     oDoc.Application.Selection.Tables[1].Rows[1].Select(); 
     oDoc.Application.Selection.InsertRowsAbove(1); 
     oDoc.Application.Selection.Tables[1].Rows[1].Select(); 

     //header row style 
     oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 1; 
     oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Tahoma"; 
     oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14; 

     //add header row manually 
     for (int c = 0; c <= ColumnCount - 1; c++) 
     { 
      oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = DGV.Columns[c].HeaderText; 
     } 

     //table style 
     oDoc.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 5"); 
     oDoc.Application.Selection.Tables[1].Rows[1].Select(); 
     oDoc.Application.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 

     //header text 
     foreach (Word.Section section in oDoc.Application.ActiveDocument.Sections) 
     { 
      Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
      headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage); 
      headerRange.Text = "your header text"; 
      headerRange.Font.Size = 16; 
      headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; 
     } 

     //save the file 
     oDoc.SaveAs2(filename); 

     //NASSIM LOUCHANI 
    } 
    } 




    private void button_Click(object sender, EventArgs e) 
    { 
    SaveFileDialog sfd = new SaveFileDialog(); 

    sfd.Filter = "Word Documents (*.docx)|*.docx"; 

    sfd.FileName = "export.docx"; 

    if (sfd.ShowDialog() == DialogResult.OK) 
    { 

     Export_Data_To_Word(dataGridView1, sfd.FileName); 
    } 
    } 

回答

1

做到這一點:

Word.Document oDoc = new Word.Document(); 

Word將不會產生一個錯誤,但文檔對象將無法正常工作。您唯一可以在Word中使用new關鍵字的時間是Word.Application。對於其他一切,請使用適當的方法。對於文檔,這將是Documents.AddDocuments.Open

您還需要聲明並實例化Word.Application的對象。例如:

Word.Application wdApp = new Word.Application(); 
wdApp.Visible = true; 

既然你想從現有的應用創建一個新文檔Add方法,指定作爲模板使用的文件的名稱:

object filePathAndName = "file info"; 
Word.Document oDoc = wdApp.Documents.Add(ref filePathAndName); 

同樣,你應該根據需要聲明對象並實例化它們,而不是依賴於Selection對象。例如:

Word.Range oRange = oDoc.Content; 
Word.Table tbl = oRange.ConvertToTable(//params here); 

非常重要的是,你還正確發佈每一個對象使用,退出應用程序,並執行明確的垃圾收集(見「釋放COM對象」 https://msdn.microsoft.com/en-us/library/office/aa679807(v=office.11).aspx);

tbl = null; 
oRange = null; 
oDoc.Close(); 
oDoc = null; 
wdApp.Quit(); 
wdApp = null; 
//GC here