WordOperation.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Word = Microsoft.Office.Interop.Word;
  5. namespace Com.Jpsoft.Hospital.ReportClass.WordReports
  6. {
  7. /// <summary>
  8. /// Word公共操作类
  9. /// </summary>
  10. public class WordOperation
  11. {
  12. private Word._Application oWordApplic;//a reference to Word application
  13. private Word._Document oDoc;//a reference to the document;
  14. public WordOperation()
  15. {
  16. //activate the interface with the COM object of Microsoft Word
  17. //实例化Word COM对象
  18. oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
  19. }
  20. //Open a file (the file must exists) and activate it
  21. /// <summary>
  22. /// 根据模板打开Word文档
  23. /// </summary>
  24. /// <param name="strFileName">模板的物理路径</param>
  25. public void Open(string strFileName)
  26. {
  27. object filename = strFileName;
  28. object readOnly = false;
  29. object isVisible = true;
  30. object missing = System.Reflection.Missing.Value;
  31. try
  32. {
  33. oDoc = oWordApplic.Documents.Open(ref filename, ref missing, ref readOnly,
  34. ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
  35. ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
  36. oDoc.Activate();
  37. }
  38. catch (Exception ex)
  39. {
  40. string errStr = ex.Message;
  41. Quit();
  42. throw ex;
  43. }
  44. }
  45. //Open a new document
  46. /// <summary>
  47. /// 打开一个新的文档
  48. /// </summary>
  49. public void Open()
  50. {
  51. object missing = System.Reflection.Missing.Value;
  52. try
  53. {
  54. oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
  55. oDoc.Activate();
  56. }
  57. catch (Exception ex)
  58. {
  59. string errStr = ex.Message;
  60. Quit();
  61. throw ex;
  62. }
  63. }
  64. /// <summary>
  65. /// 退出Word
  66. /// </summary>
  67. public void Quit()
  68. {
  69. object missing = System.Reflection.Missing.Value;
  70. if (oDoc != null)
  71. {
  72. oDoc.Close(ref missing, ref missing, ref missing);
  73. System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
  74. oDoc = null;
  75. }
  76. if (oWordApplic != null)
  77. {
  78. oWordApplic.Quit(ref missing, ref missing, ref missing);
  79. System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic);
  80. oWordApplic = null;
  81. }
  82. GC.Collect();
  83. }
  84. /// <summary>
  85. /// 保存文档
  86. /// </summary>
  87. public void Save()
  88. {
  89. try
  90. {
  91. oDoc.Save();
  92. }
  93. catch (Exception ex)
  94. {
  95. string errStr = ex.Message;
  96. Quit();
  97. throw ex;
  98. }
  99. }
  100. /// <summary>
  101. /// 把创建的文档保存到指定路径
  102. /// </summary>
  103. /// <param name="strFileName">要保存的物理路径</param>
  104. public void SaveAs(string strFileName)
  105. {
  106. object missing = System.Reflection.Missing.Value;
  107. object filename = strFileName;
  108. try
  109. {
  110. oDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
  111. ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  112. }
  113. catch (Exception ex)
  114. {
  115. string errStr = ex.Message;
  116. Quit();
  117. throw ex;
  118. }
  119. }
  120. /// <summary>
  121. /// 另存为HTML格式
  122. /// </summary>
  123. /// <param name="strFileName">保存的物理路径</param>
  124. public void SaveAsHtml(string strFileName)
  125. {
  126. object missing = System.Reflection.Missing.Value;
  127. object filename = strFileName;
  128. object Format = (int)Word.WdSaveFormat.wdFormatHTML;
  129. try
  130. {
  131. oDoc.SaveAs(ref filename, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
  132. ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  133. }
  134. catch (Exception ex)
  135. {
  136. string errStr = ex.Message;
  137. Quit();
  138. throw ex;
  139. }
  140. }
  141. /// <summary>
  142. /// 在光标处插入文本
  143. /// </summary>
  144. /// <param name="strText">要插入的文本</param>
  145. public void InsertText(string strText)
  146. {
  147. try
  148. {
  149. oWordApplic.Selection.TypeText(strText);
  150. }
  151. catch (Exception ex)
  152. {
  153. string errStr = ex.Message;
  154. Quit();
  155. throw ex;
  156. }
  157. }
  158. /// <summary>
  159. /// 插入行
  160. /// </summary>
  161. public void InsertLineBreak()
  162. {
  163. try
  164. {
  165. oWordApplic.Selection.TypeParagraph();
  166. }
  167. catch (Exception ex)
  168. {
  169. string errStr = ex.Message;
  170. Quit();
  171. throw ex;
  172. }
  173. }
  174. /// <summary>
  175. /// 插入多行
  176. /// </summary>
  177. /// <param name="nline">行数</param>
  178. public void InsertLineBreak(int nline)
  179. {
  180. try
  181. {
  182. for (int i = 0; i < nline; i++)
  183. {
  184. oWordApplic.Selection.TypeParagraph();
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. string errStr = ex.Message;
  190. Quit();
  191. throw ex;
  192. }
  193. }
  194. //Change the paragragh alignment
  195. /// <summary>
  196. /// 设置对齐格式
  197. /// </summary>
  198. /// <param name="strType">要设置的格式"Center,Left,Right,Justify"</param>
  199. public void SetAlignment(string strType)
  200. {
  201. try
  202. {
  203. switch (strType)
  204. {
  205. case "Center":
  206. oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
  207. break;
  208. case "Left":
  209. oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
  210. break;
  211. case "Right":
  212. oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
  213. break;
  214. case "Justify":
  215. oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
  216. break;
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. string errStr = ex.Message;
  222. Quit();
  223. throw ex;
  224. }
  225. }
  226. //if you use this function to change the font you should call it again with
  227. //no parameter in order to set the font without a particular format
  228. /// <summary>
  229. /// 设置字体样式
  230. /// </summary>
  231. /// <param name="strType">字体"Bold,Italic,Underlined"</param>
  232. public void SetFont(string strType)
  233. {
  234. try
  235. {
  236. switch (strType)
  237. {
  238. case "Bold":
  239. oWordApplic.Selection.Font.Bold = 1;
  240. break;
  241. case "Italic":
  242. oWordApplic.Selection.Font.Italic = 1;
  243. break;
  244. case "Underlined":
  245. oWordApplic.Selection.Font.Subscript = 0;
  246. break;
  247. }
  248. }
  249. catch (Exception ex)
  250. {
  251. string errStr = ex.Message;
  252. Quit();
  253. throw ex;
  254. }
  255. }
  256. //disable all the style
  257. /// <summary>
  258. /// 取消所有字体样式
  259. /// </summary>
  260. public void SetFont()
  261. {
  262. try
  263. {
  264. oWordApplic.Selection.Font.Bold = 0;
  265. oWordApplic.Selection.Font.Italic = 0;
  266. oWordApplic.Selection.Font.Subscript = 0;
  267. }
  268. catch (Exception ex)
  269. {
  270. string errStr = ex.Message;
  271. Quit();
  272. throw ex;
  273. }
  274. }
  275. /// <summary>
  276. /// 设置字体名
  277. /// </summary>
  278. /// <param name="strType">指定字体名</param>
  279. public void SetFontName(string strType)
  280. {
  281. try
  282. {
  283. oWordApplic.Selection.Font.Name = strType;
  284. }
  285. catch (Exception ex)
  286. {
  287. string errStr = ex.Message;
  288. Quit();
  289. throw ex;
  290. }
  291. }
  292. /// <summary>
  293. /// 设置字体大小
  294. /// </summary>
  295. /// <param name="nsize">字体大小</param>
  296. public void SetFontSize(int nsize)
  297. {
  298. try
  299. {
  300. oWordApplic.Selection.Font.Size = nsize;
  301. }
  302. catch (Exception ex)
  303. {
  304. string errStr = ex.Message;
  305. Quit();
  306. throw ex;
  307. }
  308. }
  309. /// <summary>
  310. /// 插入分页符
  311. /// </summary>
  312. public void InsertPagebreak()
  313. {
  314. try
  315. {
  316. object pBreak = (int)Word.WdBreakType.wdPageBreak;
  317. oWordApplic.Selection.InsertBreak(ref pBreak);
  318. }
  319. catch (Exception ex)
  320. {
  321. string errStr = ex.Message;
  322. Quit();
  323. throw ex;
  324. }
  325. }
  326. //Go to a predefineed bookmark,if the bookmark doesn't exists the application will raise an error
  327. /// <summary>
  328. /// 光标移动到书签处
  329. /// </summary>
  330. /// <param name="strBookMarkName">书签名</param>
  331. public void GotoBookMark(string strBookMarkName)
  332. {
  333. try
  334. {
  335. object missing = System.Reflection.Missing.Value;
  336. object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
  337. object NameBookMark = strBookMarkName;
  338. oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
  339. }
  340. catch (Exception ex)
  341. {
  342. string errStr = ex.Message;
  343. Quit();
  344. throw ex;
  345. }
  346. }
  347. /// <summary>
  348. /// 光标移动到结尾处
  349. /// </summary>
  350. public void GoToTheEnd()
  351. {
  352. try
  353. {
  354. object missing = System.Reflection.Missing.Value;
  355. object unit;
  356. unit = Word.WdUnits.wdStory;
  357. oWordApplic.Selection.EndKey(ref unit, ref missing);
  358. }
  359. catch (Exception ex)
  360. {
  361. string errStr = ex.Message;
  362. Quit();
  363. throw ex;
  364. }
  365. }
  366. /// <summary>
  367. /// 光标移动到开始处
  368. /// </summary>
  369. public void GoToTheBeginning()
  370. {
  371. try
  372. {
  373. object missing = System.Reflection.Missing.Value;
  374. object unit;
  375. unit = Word.WdUnits.wdStory;
  376. oWordApplic.Selection.HomeKey(ref unit, ref missing);
  377. }
  378. catch (Exception ex)
  379. {
  380. string errStr = ex.Message;
  381. Quit();
  382. throw ex;
  383. }
  384. }
  385. /// <summary>
  386. /// 光标移动到表格
  387. /// </summary>
  388. /// <param name="ntable"></param>
  389. public void GoToTheTable(int ntable)
  390. {
  391. try
  392. {
  393. object missing = System.Reflection.Missing.Value;
  394. object what;
  395. what = Word.WdUnits.wdTable;
  396. object which;
  397. which = Word.WdGoToDirection.wdGoToFirst;
  398. object count;
  399. count = 1;
  400. oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
  401. oWordApplic.Selection.Find.ClearFormatting();
  402. oWordApplic.Selection.Text = "";
  403. }
  404. catch (Exception ex)
  405. {
  406. string errStr = ex.Message;
  407. Quit();
  408. throw ex;
  409. }
  410. }
  411. /// <summary>
  412. /// 光标移动到右方单元格
  413. /// </summary>
  414. public void GoToRightCell()
  415. {
  416. try
  417. {
  418. object missing = System.Reflection.Missing.Value;
  419. object direction;
  420. direction = Word.WdUnits.wdCell;
  421. oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
  422. }
  423. catch (Exception ex)
  424. {
  425. string errStr = ex.Message;
  426. Quit();
  427. throw ex;
  428. }
  429. }
  430. /// <summary>
  431. /// 光标移动到左方单元格
  432. /// </summary>
  433. public void GoToLeftCell()
  434. {
  435. try
  436. {
  437. object missing = System.Reflection.Missing.Value;
  438. object direction;
  439. direction = Word.WdUnits.wdCell;
  440. oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
  441. }
  442. catch (Exception ex)
  443. {
  444. string errStr = ex.Message;
  445. Quit();
  446. throw ex;
  447. }
  448. }
  449. /// <summary>
  450. /// 光标移动到下方单元格
  451. /// </summary>
  452. public void GoToDownCell()
  453. {
  454. try
  455. {
  456. object missing = System.Reflection.Missing.Value;
  457. object direction;
  458. direction = Word.WdUnits.wdCell;
  459. oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
  460. }
  461. catch (Exception ex)
  462. {
  463. string errStr = ex.Message;
  464. Quit();
  465. throw ex;
  466. }
  467. }
  468. /// <summary>
  469. /// 光标移动到上方单元格
  470. /// </summary>
  471. public void GoToUpCell()
  472. {
  473. try
  474. {
  475. object missing = System.Reflection.Missing.Value;
  476. object direction;
  477. direction = Word.WdUnits.wdCell;
  478. oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
  479. }
  480. catch (Exception ex)
  481. {
  482. string errStr = ex.Message;
  483. Quit();
  484. throw ex;
  485. }
  486. }
  487. }
  488. }