用C#动态生成Word文档并将数据填入Word表格中

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

用C#动态生成Word文档并将数据填入Word表格中

2009-07-16 22:53 125人阅读评论(0) 收藏举报刚刚实现了个功能:用C#实现动态生成Word文档,在Word文档中插入表格,并将读出的数据填入到表格中。

要使用C#操作word,首先要添加引用:

1、添加引用->COM->Microsoft Word 11.0 Object Library

2、在.cs文件中添加

using Word;

下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)

[c-sharp]v iew plaincopy print?

1. public string CreateWordFile(string CheckedInf o)

2. ...{

3. string message = "";

4. try

5. ...{

6. Object Nothing = Sy stem.Reflection.Missing.Value;

7. Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录

8. string name = "CNSI_" + DateTime.Now.ToShortString()+".doc";

9. object f ilename = "C://CNSI//" + name; //文件保存路径

10. //创建Word文档

11. Word.Application WordApp = new Word.ApplicationClass();

12. Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

13.

14. //添加页眉

15. WordApp.Activ eWindow.View.Ty pe = WdViewTy pe.wdOutlineView;

16. WordApp.Activ eWindow.View.SeekView = WdSeekView.wdSeekPrimary Header;

17. WordApp.Activ eWindow.Activ ePane.Selection.InsertAfter("[页眉内容]");

18. WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右

对齐

19. WordApp.Activ eWindow.View.SeekView = WdSeekView.wdSeek MainDocument;//跳出页眉设置

20.

21. WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

22.

23. //移动焦点并换行

24. object count = 14;

25. object WdLine = Word.WdUnits.wdLine;//换一行;

26. WordApp.Selection.Mov eDown(ref WdLine, ref count, ref Nothing);//移动焦点

27. WordApp.Selection.Ty peParagraph();//插入段落

28.

29. //文档中创建表格

30. Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);

31. //设置表格样式

32. newTable.Borders.OutsideLineSty le = Word.WdLineSty le.wdLineSty leThickThinLargeGap;

33. newTable.Borders.InsideLineSty le = Word.WdLineSty le.wdLineSty leSingle;

34. newTable.Columns[1].Width = 100f;

35. newTable.Columns[2].Width = 220f;

36. newTable.Columns[3].Width = 105f;

37.

38. //填充表格内容

39. newTable.Cell(1, 1).Range.Text = "产品详细信息表";

40. newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体

41. //合并单元格

42. newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));

43. WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居

44. WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平

居中

45.

46. //填充表格内容

47. newTable.Cell(2, 1).Range.Text = "产品基本信息";

48. newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色

49. //合并单元格

50. newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));

51. WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

52.

53. //填充表格内容

54. newTable.Cell(3, 1).Range.Text = "品牌名称:";

55. newTable.Cell(3, 2).Range.Text = BrandName;

56. //纵向合并单元格

57. newTable.Cell(3, 3).Select();//选中一行

58. object mov eUnit = Word.WdUnits.wdLine;

59. object mov eCount = 5;

60. object mov eExtend = Word.WdMov ementT y pe.wdExtend;

61. WordApp.Selection.Mov eDown(ref mov eUnit, ref mov eCount, ref mov eExtend);

62. WordApp.Selection.Cells.Merge();

63. //插入图片

64. string FileName = Picture;//图片所在路径

65. object LinkToFile = f alse;

66. object Sav eWithDocument = true;

67. object Anchor = WordDoc.Application.Selection.Range;

相关文档
最新文档