利用C#向word文档中写入数据

一.新建WORD模板 完全安装Word 2003

       新建一个word,选中视图--工具栏--窗体,点击文字型窗体域添加一个域。双击新添加的域,默认文字填写你要显示的文字,域设置--书签填入一个标签名,用于在程序中识别这个域

  二.添加COM引用---Microsoft  Word  11.0  Object Library

  三 .添加命名空间---using Microsoft.Office.Interop.Word;

  四.定义对象

       ApplicationClass app = null;//定义应用程序对象         Document doc = null;        //定义word文档对象         Object missing = System.Reflection.Missing.Value;//定义空变量         Object isReadOnly = false;  五.方法

1)打开word模板

  1. /// <summary>   
  2.   
  3. /// 打开模板word文档   
  4.   
  5. /// </summary>   
  6.   
  7. /// <param name="parFilePath">文档路径</param>   
  8.   
  9. private void OpenDocument(string parFilePath)   
  10.   
  11. {   
  12.   
  13. object filePath = parFilePath;//文档路径   
  14.   
  15. app = new ApplicationClass();   
  16.   
  17. //打开文档   
  18.   
  19. doc = app.Documents.Open(ref filePath, ref missing, ref isReadOnly, ref missing, ref missing,   
  20.   
  21.            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,   
  22.   
  23.            ref missing, ref missing, ref missing, ref missing);   
  24.   
  25.            doc.Activate();//激活文档   
  26.   
  27. }  
  1. 2.向word文档写入数据  
  1. <PRE class=csharp name="code">/// <summary>   
  2.   
  3. /// 向word文档写入数据   
  4.   
  5. /// </summary>   
  6.   
  7. /// <param name="parLableName">域标签</param>   
  8.   
  9. /// <param name="parFillName">写入域中的内容</param>   
  10.   
  11. private void WriteIntoDocument(string parLableName, string parFillName)   
  12.   
  13. {   
  14.   
  15. object lableName = parLableName;   
  16.   
  17. Bookmark bm = doc.Bookmarks.get_Item(ref lableName);//返回标签   
  18.   
  19. bm.Range.Text = parFillName;//设置域标签的内容   
  20.   
  21. }</PRE>   
  22. <PRE class=csharp name="code"> </PRE>   
  23. <PRE class=csharp name="code">3.保存并关闭</PRE>   
  24. <PRE class=csharp name="code"><PRE class=csharp name="code"/// <summary>   
  25.   
  26. /// 保存并关闭   
  27.   
  28. /// </summary>   
  29.   
  30. /// <param name="parSaveDocPath">文档另存为的路径</param>   
  31.   
  32. private void SaveAndClose(string parSaveDocPath)   
  33.   
  34.  {   
  35.   
  36. object savePath = parSaveDocPath;//文档另存为的路径   
  37.   
  38. Object saveChanges = app.Options.BackgroundSave;//关闭doc文档不提示保存   
  39.   
  40. //文档另存为   
  41.   
  42. doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,   
  43.   
  44.                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);   
  45.   
  46. doc.Close(ref saveChanges, ref missing, ref missing);//关闭文档   
  47.   
  48. app.Quit(ref missing, ref missing, ref missing);     //关闭应用程序   
  49.   
  50. }</PRE>   
  51. </PRE>  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先机子上安装有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";//接收指令条数 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 Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.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 Nothing, 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 Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值