WordHelperTester.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Microsoft.Office.Interop.Word;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Y.Utils.OfficeUtils
  6. {
  7. class WordHelperTester
  8. {
  9. static void Main(string[] args)
  10. {
  11. int tsize = 5;
  12. List<Dictionary<string, object>> test = new List<Dictionary<string, object>>();
  13. for (int i = 0; i < 10; i++)
  14. {
  15. Dictionary<string, object> q = new Dictionary<string, object>();
  16. q.Add("title", string.Format("第{0}题:{0}{0}{0}{0}", i));
  17. q.Add("content", "长期从事电脑操作者,应多吃一些新鲜的蔬菜和水果,同时增加维生素A、B1、C、E的摄入。为预防角膜干燥、眼干涩、视力下降、甚至出现夜盲等,电 脑操作者应多吃富含维生素A的食物,如豆制品、鱼、牛奶、核桃、青菜、大白菜、空心菜、西红柿及新鲜水果等。");
  18. q.Add("image", @"C:\Users\Administrator\Pictures\1.jpg");
  19. test.Add(q);
  20. }
  21. WordHelper report = new WordHelper();
  22. report.CreateNewDocument(@"F:\模板1.docx"); //模板路径
  23. report.InsertValue("name", "测试考试");//在书签“Bookmark_value”处插入值
  24. Table table = report.InsertTable("content", test.Count() * tsize, 1, 0); //在书签“Bookmark_table”处插入2行3列行宽最大的表
  25. report.UseBorder(1, 0); //模板中第一个表格使用实线边框
  26. for (int i = 0; i < test.Count(); i++)
  27. {
  28. //string picturePath = t["image"].ToString();
  29. //report.InsertPicture("content", picturePath, 150, 150); //书签位置,图片路径,图片宽度,图片高度
  30. //string content = t["content"].ToString();
  31. //report.InsertText("content", content);
  32. //string title = t["title"].ToString();
  33. //report.InsertText("content", title);
  34. report.InsertCell(table, 1 + (i * tsize), 1, test[i]["title"].ToString());//表名,行号,列号,值
  35. report.InsertCell(table, 2 + (i * tsize), 1, test[i]["content"].ToString());//表名,行号,列号,值
  36. report.InsertCell(table, 3 + (i * tsize), 1, test[i]["image"].ToString(), 150, 150, i + 1);//表名,行号,列号,值
  37. }
  38. report.SaveDocument(@"F:\doc1.doc"); //文档路径
  39. Console.WriteLine("please input enter to exit.");
  40. Console.ReadLine();
  41. }
  42. }
  43. }