WordTool.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ////############################################################
  2. //// https://github.com/yuzhengyang
  3. //// author:yuzhengyang
  4. ////############################################################
  5. //using Microsoft.Office.Interop.Word;
  6. //using System;
  7. //using System.Collections.Generic;
  8. //using System.Linq;
  9. //using System.Text;
  10. //using System.Threading.Tasks;
  11. //namespace Y.Utils.IOUtils.OfficeUtils
  12. //{
  13. // public class WordTool
  14. // {
  15. // private _Application wordApp = null;
  16. // private _Document wordDoc = null;
  17. // public _Application Application
  18. // {
  19. // get
  20. // {
  21. // return wordApp;
  22. // }
  23. // set
  24. // {
  25. // wordApp = value;
  26. // }
  27. // }
  28. // public _Document Document
  29. // {
  30. // get
  31. // {
  32. // return wordDoc;
  33. // }
  34. // set
  35. // {
  36. // wordDoc = value;
  37. // }
  38. // }
  39. // //通过模板创建新文档
  40. // public void CreateNewDocument(string filePath)
  41. // {
  42. // killWinWordProcess();
  43. // wordApp = new Application();
  44. // wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
  45. // wordApp.Visible = false;
  46. // object missing = System.Reflection.Missing.Value;
  47. // object templateName = filePath;
  48. // wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
  49. // ref missing, ref missing, ref missing, ref missing, ref missing,
  50. // ref missing, ref missing, ref missing, ref missing, ref missing,
  51. // ref missing, ref missing, ref missing, ref missing);
  52. // }
  53. // //保存新文件
  54. // public void SaveDocument(string filePath)
  55. // {
  56. // object fileName = filePath;
  57. // object format = WdSaveFormat.wdFormatDocument;//保存格式
  58. // object miss = System.Reflection.Missing.Value;
  59. // wordDoc.SaveAs(ref fileName, ref format, ref miss,
  60. // ref miss, ref miss, ref miss, ref miss,
  61. // ref miss, ref miss, ref miss, ref miss,
  62. // ref miss, ref miss, ref miss, ref miss,
  63. // ref miss);
  64. // //关闭wordDoc,wordApp对象
  65. // object SaveChanges = WdSaveOptions.wdSaveChanges;
  66. // object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
  67. // object RouteDocument = false;
  68. // wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
  69. // wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
  70. // }
  71. // //在书签处插入值
  72. // public bool InsertValue(string bookmark, string value)
  73. // {
  74. // object bkObj = bookmark;
  75. // if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
  76. // {
  77. // wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
  78. // wordApp.Selection.TypeText(value);
  79. // return true;
  80. // }
  81. // return false;
  82. // }
  83. // //插入表格,bookmark书签
  84. // public Table InsertTable(string bookmark, int rows, int columns, float width)
  85. // {
  86. // object miss = System.Reflection.Missing.Value;
  87. // object oStart = bookmark;
  88. // Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置
  89. // Table newTable = wordDoc.Tables.Add(range, rows, columns, ref miss, ref miss);
  90. // //设置表的格式
  91. // newTable.Borders.Enable = 1; //允许有边框,默认没有边框(为0时报错,1为实线边框,2、3为虚线边框,以后的数字没试过)
  92. // newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;//边框宽度
  93. // if (width != 0)
  94. // {
  95. // newTable.PreferredWidth = width;//表格宽度
  96. // }
  97. // newTable.AllowPageBreaks = false;
  98. // return newTable;
  99. // }
  100. // //合并单元格 表名,开始行号,开始列号,结束行号,结束列号
  101. // public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2)
  102. // {
  103. // table.Cell(row1, column1).Merge(table.Cell(row2, column2));
  104. // }
  105. // //设置表格内容对齐方式Align水平方向,Vertical垂直方向(左对齐,居中对齐,右对齐分别对应Align和Vertical的值为-1,0,1)
  106. // public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align, int Vertical)
  107. // {
  108. // switch (Align)
  109. // {
  110. // case -1:
  111. // table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break;//左对齐
  112. // case0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break;//水平居中
  113. // case1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break;//右对齐
  114. // }
  115. // switch (Vertical)
  116. // {
  117. // case -1:
  118. // table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break;//顶端对齐
  119. // case0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break;//垂直居中
  120. // case1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break;//底端对齐
  121. // }
  122. // }
  123. // //设置表格字体
  124. // public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size)
  125. // {
  126. // if (size != 0)
  127. // {
  128. // table.Range.Font.Size = Convert.ToSingle(size);
  129. // }
  130. // if (fontName != "")
  131. // {
  132. // table.Range.Font.Name = fontName;
  133. // }
  134. // }
  135. // //是否使用边框,n表格的序号,use是或否
  136. // public void UseBorder(int n, bool use)
  137. // {
  138. // if (use)
  139. // {
  140. // wordDoc.Content.Tables[n].Borders.Enable = 1; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
  141. // }
  142. // else
  143. // {
  144. // wordDoc.Content.Tables[n].Borders.Enable = 2; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
  145. // }
  146. // }
  147. // //设置表格边框
  148. // public void UseBorder(int n, int type)
  149. // {
  150. // wordDoc.Content.Tables[n].Borders.Enable = type; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过)
  151. // }
  152. // //给表格插入一行,n表格的序号从1开始记
  153. // public void AddRow(int n)
  154. // {
  155. // object miss = System.Reflection.Missing.Value;
  156. // wordDoc.Content.Tables[n].Rows.Add(ref miss);
  157. // }
  158. // //给表格添加一行
  159. // public void AddRow(Microsoft.Office.Interop.Word.Table table)
  160. // {
  161. // object miss = System.Reflection.Missing.Value;
  162. // table.Rows.Add(ref miss);
  163. // }
  164. // //给表格插入rows行,n为表格的序号
  165. // public void AddRow(int n, int rows)
  166. // {
  167. // object miss = System.Reflection.Missing.Value;
  168. // Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
  169. // for (int i = 0; i < rows; i++)
  170. // {
  171. // table.Rows.Add(ref miss);
  172. // }
  173. // }
  174. // //给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素
  175. // public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string value)
  176. // {
  177. // table.Cell(row, column).Range.Text = value;
  178. // }
  179. // //给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素
  180. // public void InsertCell(int n, int row, int column, string value)
  181. // {
  182. // wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;
  183. // }
  184. // //给表格插入一行数据,n为表格的序号,row行号,columns列数,values插入的值
  185. // public void InsertCell(int n, int row, int columns, string[] values)
  186. // {
  187. // Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
  188. // for (int i = 0; i < columns; i++)
  189. // {
  190. // table.Cell(row, i + 1).Range.Text = values[i];
  191. // }
  192. // }
  193. // //插入图片
  194. // public void InsertPicture(string bookmark, string picturePath, float width, float hight)
  195. // {
  196. // object miss = System.Reflection.Missing.Value;
  197. // object oStart = bookmark;
  198. // Object linkToFile = false; //图片是否为外部链接
  199. // Object saveWithDocument = true; //图片是否随文档一起保存
  200. // object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//图片插入位置
  201. // wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
  202. // wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width; //设置图片宽度
  203. // wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight; //设置图片高度
  204. // }
  205. // //在表格中插入图片-Y
  206. // public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string picturePath, float width, float hight, int idx)
  207. // {
  208. // Object linkToFile = false; //图片是否为外部链接
  209. // Object saveWithDocument = true; //图片是否随文档一起保存
  210. // object range = table.Cell(row, column).Range;//图片插入位置
  211. // wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
  212. // wordDoc.Application.ActiveDocument.InlineShapes[idx].Width = width; //设置图片宽度
  213. // wordDoc.Application.ActiveDocument.InlineShapes[idx].Height = hight; //设置图片高度
  214. // }
  215. // //插入一段文字,text为文字内容
  216. // public void InsertText(string bookmark, string text)
  217. // {
  218. // object oStart = bookmark;
  219. // object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
  220. // Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range);
  221. // wp.Format.SpaceBefore = 6;
  222. // wp.Range.Text = text;
  223. // wp.Format.SpaceAfter = 24;
  224. // wp.Range.InsertParagraphAfter();
  225. // wordDoc.Paragraphs.Last.Range.Text = "\n";
  226. // }
  227. // //杀掉winword.exe进程
  228. // public void killWinWordProcess()
  229. // {
  230. // System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
  231. // foreach (System.Diagnostics.Process process in processes)
  232. // {
  233. // bool b = process.MainWindowTitle == "";
  234. // if (process.MainWindowTitle == "")
  235. // {
  236. // process.Kill();
  237. // }
  238. // }
  239. // }
  240. // }
  241. //}