C#導出WORD


一、操作Word 

  首先引用這個DLL,Microsoft.Office.Interop.Word,官方提供的。

  可以操作word文字,表格,圖片等。

  文字通過替換關鍵字的方式實現

  document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");

  表格可以自己獲取模板中已有的表格

   Microsoft.Office.Interop.Word.Table table1 = document.Tables[1];

   table1.Cell(1, 1).Range.Text = "TEST1";

  也可以自己創建表格,可以設計表頭,單元格等。

 

   int tableRow = 6 ; int tableColumn = 6; //定義一個Word中的表格對象 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range, tableRow, tableColumn, ref Nothing, ref Nothing); 
   //默認創建的表格沒有邊框,這里修改其屬性,使得創建的表格帶有邊框 table.Borders.Enable = 1; table.Cell(1, 1).Merge(table.Cell(2, 1));//縱向合並 table.Cell(1, 1).Range.Text = "牌號/代碼"; table.Cell(1, 2).Merge(table.Cell(2, 2));//縱向合並 table.Cell(1, 2).Range.Text = "標准編號";
  有一篇文章寫的很詳細可以參考下:https://www.cnblogs.com/xh6300/p/5915717.html

 

復制代碼
public bool CreateWord(DataTable dttmp) { bool result = false; Object Nothing = Missing.Value; Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document document = null; string path = @"C:\Users\Administrator\Desktop\BB\合同模版.doc"; object FileName = @"C:\Users\Administrator\Desktop\BB\" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".doc"; application.Visible = false; document = application.Documents.Open(path); int rowNum = dttmp.Rows.Count; for (int i = 1; i <= document.Paragraphs.Count; i++) { string temptext = document.Paragraphs[i].Range.Text; //以下為替換文檔模版中的關鍵字 if (temptext.Contains("{$village}")) document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD"); Microsoft.Office.Interop.Word.Table table1 = document.Tables[1]; table1.Cell(1, 1).Range.Text = "TEST1"; if (temptext.Contains("{$Table1}")) { //設置表格的行數和列數 int tableRow = 6 + rowNum; int tableColumn = 13; //定義一個Word中的表格對象  Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range, tableRow, tableColumn, ref Nothing, ref Nothing); //默認創建的表格沒有邊框,這里修改其屬性,使得創建的表格帶有邊框 table.Borders.Enable = 1;//這個值可以設置得很大 table.Cell(1, 1).Merge(table.Cell(2, 1));//縱向合並 table.Cell(1, 1).Range.Text = "牌號/代碼"; table.Cell(1, 2).Merge(table.Cell(2, 2));//縱向合並 table.Cell(1, 2).Range.Text = "標准編號"; table.Cell(1, 6).Merge(table.Cell(2, 6)); table.Cell(1, 6).Range.Text = "單重\n(MT)"; table.Cell(1, 7).Merge(table.Cell(2, 7)); table.Cell(1, 7).Range.Text = "張數"; table.Cell(1, 8).Merge(table.Cell(2, 8)); table.Cell(1, 8).Range.Text = "重量\n(MT))"; table.Cell(1, 9).Merge(table.Cell(2, 9)); table.Cell(1, 9).Range.Text = "單價\n(元/噸)"; table.Cell(1, 10).Merge(table.Cell(2, 10)); table.Cell(1, 10).Range.Text = "金額(人民幣)"; table.Cell(1, 13).Merge(table.Cell(2, 13)); table.Cell(1, 13).Range.Text = "交貨期"; table.Cell(1, 3).Merge(table.Cell(1, 5));//橫向合並 table.Cell(1, 3).Range.Text = "規格(mm)"; table.Cell(2, 3).Range.Text = "厚度"; table.Cell(2, 4).Range.Text = "寬度"; table.Cell(2, 5).Range.Text = "寬度"; table.Cell(1, 9).Merge(table.Cell(1, 10)); table.Cell(1, 10).Range.Text = "表面加工"; table.Cell(2, 11).Range.Text = "邊緣"; table.Cell(2, 11).Range.Text = "精度"; } } object format = document.SaveFormat; document.Save(); application.Quit(ref Nothing, ref Nothing, ref Nothing); return result; }
復制代碼

二、Word導出PDF

  

復制代碼
 public bool WordToPDF(string sourcePath) { bool result = false; Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document document = null; try { application.Visible = false; document = application.Documents.Open(sourcePath); string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置 if (!File.Exists(@PDFPath))//存在PDF,不需要繼續轉換  { document.ExportAsFixedFormat(PDFPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); } result = true; } catch (Exception e) { Console.WriteLine(e.Message); result = false; } finally { //document.Close();  } return result; }
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM