logo资料库

C#实现word文档打印及预览.docx

第1页 / 共3页
第2页 / 共3页
第3页 / 共3页
资料共3页,全文预览结束
首先机子上安装有 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.StartupPat h + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), tru e);//模板 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 Not hing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumber Alignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignm ent.wdAlignPageNumberCenter;
myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.W ord.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignm ent, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.W ord.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = M icrosoft.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";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Not hing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
object savechanges = Microsoft.Office.Interop.Word.WdSaveOption s.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(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, ref Nothing, ref Noth ing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref No thing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothin g, ref Nothing); doc.PrintPreview();//打印预览
分享到:
收藏