首先机子上安装有office,在COM中添加引用Microsoft.Word.11.0.Object.Library(或11.0以上)
Microsoft.Office.Interop.Word.Application myWordApp = null;
Microsoft.Office.Interop.Word.Document doc = null;
object Filename = path + "\\" + TaskID + ".docx";//目的文件
object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表
System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD中有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率
myWordApp = new Microsoft.Office.Interop.Word.Application();
doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
/////显示页码
object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
object oFirstPage = true;
oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage);
myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
for (int i = 2; i < 102; i++)//举例100台
{
doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行
doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号
doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号
doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数
doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数
1