First, the operating Word
First reference this DLL, Microsoft.Office.Interop.Word, the official offer.
You can operate word of text, tables, pictures and so on.
Text by way of replacement keyword implementation
document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");
Get your own template forms can already form
Microsoft.Office.Interop.Word.Table table1 = document.Tables[1];
table1.Cell(1, 1).Range.Text = "TEST1";
You can also create your own table, you can design the header, cell and so on.
TableRow = int . 6 ; int = TableColumn . 6 ; // definition of a table object in Word Microsoft.Office.Interop.Word.Table = Table the Document.Tables.Add (document.Paragraphs [I] .Range, TableRow, TableColumn, REF Nothing, REF Nothing);
// default table created no border, where modify its properties, such that the table is created with a border
table.Borders.Enable = . 1; table.Cell ( . 1, . 1) .Merge (table.Cell ( 2, . 1)); // vertical integration table.Cell ( . 1, . 1) .Range.Text = " grade / Code " ; table.Cell ( . 1, 2) .Merge (table.Cell ( 2, 2)); // vertical merger table. the Cell ( . 1, 2) = .Range.Text " standard number " ;
There is an article written in great detail can refer to the following: 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 ( " {} $ the Table1 " )) { // set the table number of rows and columns int = TableRow . 6 + rowNum; int = TableColumn 13 is ; // definition of a table object in Word Microsoft.Office. = table Interop.Word.Table the Document.Tables.Add (document.Paragraphs [I] .Range, TableRow, TableColumn, REF Nothing, REF Nothing); // default table created no border, where modify its properties, such that the created table with borders = table.Borders.Enable . 1; // large value can be set very table.Cell ( . 1, . 1) .Merge (table.Cell ( 2, . 1)); // vertical merger table.Cell ( . 1, . 1) = .Range.Text "Grade / Code " ; table.Cell ( . 1, 2) .Merge (table.Cell ( 2, 2)); // longitudinal combined table.Cell ( . 1, 2) = .Range.Text " standard number " ; Table. the Cell ( . 1, . 6) .Merge (table.Cell ( 2, . 6 )); table.Cell ( . 1, . 6) .Range.Text = " singlet \ n-(the MT) " ; table.Cell ( . 1, . 7) .Merge (table.Cell ( 2, . 7 table.Cell (;)) . 1, . 7) .Range.Text = " number of copies " ; 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)); // horizontal mergers table.Cell ( 1 . 3) .Range.Text = " Size (mm) " ; table.Cell ( 2, . 3) .Range.Text = " thickness " ; table.Cell ( 2, . 4) .Range.Text = " width " ; table.Cell ( 2, . 5) .Range.Text = " width " ; table.Cell ( . 1, . 9) .Merge (table.Cell ( . 1, 10 ) ); table.Cell ( . 1, 10) = .Range.Text " surface processing " ; table.Cell ( 2, . 11) .Range.Text = " edge "; table.Cell(2, 11).Range.Text = "精度"; } } object format = document.SaveFormat; document.Save(); application.Quit(ref Nothing, ref Nothing, ref Nothing); return result; }
Two, Word Export 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; }