C#操作Word表格的彪悍实例 很全

最近由于工作需要,做了一些关于C#操作Word表格文档方面的工作.主要是是C#操作Word表格的操作,以下是部分代码源码天空,关于操作不规则表格的.那么让我们来看看具体的实施吧:

 
 
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. using System.Configuration;
  12. using System.IO;
  13. using System.Reflection;
  14. using System.Runtime.InteropServices ;
  15. using System.Threading;
  16. //C#操作Word表格
  17. public void MakeMyTable(DataTable DT,string strFilePath)
  18. {
  19. string strEnd = this.txtEnd.Text.Trim().ToString();
  20. string strStart = this.txtStart.Text.Trim().ToString();
  21. //生成文档分页中的起始和终止页
  22. string strSign = "("+strStart + "-" + strEnd + ")";
  23. //杀掉所有word进程以保证速度
  24. //KillWordProcess();
  25. object Nothing = System.Reflection.Missing.Value;
  26. object missing = System.Reflection.Missing.Value;
  27. object filename= strFilePath;
  28. Word.Application wordApp=new Word.ApplicationClass();
  29. Word.Document wordDoc=wordApp.Documents.Add(
  30. ref Nothing,ref Nothing,ref Nothing,ref Nothing);
  31. //C#操作Word表格
  32. try
  33. {
  34. //生成过程中屏蔽返回按扭,不允许中途停止
  35. Button2.Enabled = false;
  36. #region 生成文档
  37. //设置文档宽度
  38. wordApp.Selection.PageSetup.LeftMargin =
  39. wordApp.CentimetersToPoints(float.Parse("2"));
  40. wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11 ;
  41. wordApp.Selection.PageSetup.RightMargin =
  42. wordApp.CentimetersToPoints(float.Parse("2"));
  43. Object start = Type.Missing;
  44. Object end = Type.Missing;
  45. Object unit = Type.Missing;
  46. Object count = Type.Missing;
  47. wordDoc.Range(ref start, ref end).
  48. Delete(ref unit, ref count);
  49. //C#操作Word表格
  50. object rng = Type.Missing;
  51. string strInfo = this.txtNameCh.
  52. Text.ToString()+"明细表"+strSign+"\r\n";
  53. start = 0;
  54. end = 0;
  55. wordDoc.Range(ref start, ref end).
  56. InsertBefore(strInfo);
  57. wordDoc.Range(ref start, ref end).
  58. Font.Name = "Verdana";
  59. wordDoc.Range(ref start, ref end).
  60. Font.Size = 20;
  61. wordDoc.Range(ref start, ref end).
  62. ParagraphFormat.Alignment = Word.WdParagraphAlignment.
  63. wdAlignParagraphCenter;
  64. start = 8;
  65. end = strInfo.Length;
  66. wordDoc.Range(ref start, ref end).
  67. InsertParagraphAfter();//插入回车
  68. if(DT.Rows.Count>0)
  69. {
  70. //C#操作Word表格之存在数据项
  71. //添加一个表格
  72. object missingValue = Type.Missing;
  73. object location = strInfo.Length;
  74. //注:若location超过已有字符的长度将会出错。一定要比"明细表"串多一个字符
  75. Word.Range rng2 = wordDoc.Range(ref location, ref location);
  76. wordDoc.Tables.Add(rng2, 13, 6, ref missingValue, ref missingValue);
  77. wordDoc.Tables.Item(1).Rows.HeightRule =
  78. Word.WdRowHeightRule.wdRowHeightAtLeast;
  79. wordDoc.Tables.Item(1).Rows.Height = wordApp.
  80. CentimetersToPoints(float.Parse("0.8"));
  81. wordDoc.Tables.Item(1).Range.Font.Size = 10;
  82. wordDoc.Tables.Item(1).Range.Font.Name = "宋体";
  83. wordDoc.Tables.Item(1).Range.ParagraphFormat.Alignment =
  84. Word.WdParagraphAlignment.wdAlignParagraphCenter;
  85. wordDoc.Tables.Item(1).Range.Cells.VerticalAlignment =
  86. Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
  87. //C#操作Word表格之设置表格样式
  88. wordDoc.Tables.Item(1).Borders.
  89. Item(Word.WdBorderType.wdBorderLeft).LineStyle =
  90. Word.WdLineStyle.wdLineStyleSingle;
  91. wordDoc.Tables.Item(1).Borders.
  92. Item(Word.WdBorderType.wdBorderLeft).LineWidth =
  93. Word.WdLineWidth.wdLineWidth050pt;
  94. wordDoc.Tables.Item(1).Borders.
  95. Item(Word.WdBorderType.wdBorderLeft).Color =
  96. Word.WdColor.wdColorAutomatic;
  97. wordDoc.Tables.Item(1).Borders.
  98. Item(Word.WdBorderType.wdBorderTop).LineStyle =
  99. Word.WdLineStyle.wdLineStyleSingle;
  100. wordDoc.Tables.Item(1).Borders.
  101. Item(Word.WdBorderType.wdBorderTop).LineWidth =
  102. Word.WdLineWidth.wdLineWidth050pt;
  103. wordDoc.Tables.Item(1).Borders.
  104. Item(Word.WdBorderType.wdBorderTop).Color =
  105. Word.WdColor.wdColorAutomatic;
  106. wordDoc.Tables.Item(1).Borders.
  107. Item(Word.WdBorderType.wdBorderBottom).LineStyle =
  108. Word.WdLineStyle.wdLineStyleSingle;
  109. wordDoc.Tables.Item(1).Borders.
  110. Item(Word.WdBorderType.wdBorderBottom).LineWidth =
  111. Word.WdLineWidth.wdLineWidth050pt;
  112. wordDoc.Tables.Item(1).Borders.
  113. Item(Word.WdBorderType.wdBorderBottom).Color =
  114. Word.WdColor.wdColorAutomatic;
  115. wordDoc.Tables.Item(1).Borders.
  116. Item(Word.WdBorderType.wdBorderHorizontal).LineStyle =
  117. Word.WdLineStyle.wdLineStyleSingle;
  118. wordDoc.Tables.Item(1).Borders.
  119. Item(Word.WdBorderType.wdBorderHorizontal).LineWidth =
  120. Word.WdLineWidth.wdLineWidth050pt;
  121. wordDoc.Tables.Item(1).Borders.
  122. Item(Word.WdBorderType.wdBorderHorizontal).Color =
  123. Word.WdColor.wdColorAutomatic;
  124. wordDoc.Tables.Item(1).Borders.
  125. Item(Word.WdBorderType.wdBorderVertical).LineStyle =
  126. Word.WdLineStyle.wdLineStyleSingle;
  127. wordDoc.Tables.Item(1).Borders.
  128. Item(Word.WdBorderType.wdBorderVertical).LineWidth =
  129. Word.WdLineWidth.wdLineWidth050pt;
  130. wordDoc.Tables.Item(1).Borders.
  131. Item(Word.WdBorderType.wdBorderVertical).Color =
  132. Word.WdColor.wdColorAutomatic;
  133. wordDoc.Tables.Item(1).Borders.
  134. Item(Word.WdBorderType.wdBorderRight).LineStyle =
  135. Word.WdLineStyle.wdLineStyleSingle;
  136. wordDoc.Tables.Item(1).Borders.
  137. Item(Word.WdBorderType.wdBorderRight).LineWidth =
  138. Word.WdLineWidth.wdLineWidth050pt;
  139. wordDoc.Tables.Item(1).Borders.
  140. Item(Word.WdBorderType.wdBorderRight).Color =
  141. Word.WdColor.wdColorAutomatic;
  142. //C#操作Word表格
  143. //wordDoc.Tables.Item(k).Borders.
  144. Item(Word.WdBorderType.wdBorderDiagonalDown).
  145. LineStyle = Word.WdLineStyle.wdLineStyleSingle;
  146. //wordDoc.Tables.Item(k).Borders.
  147. Item(Word.WdBorderType.wdBorderDiagonalDown).
  148. LineWidth = Word.WdLineWidth.wdLineWidth050pt;
  149. //wordDoc.Tables.Item(k).Borders.
  150. Item(Word.WdBorderType.wdBorderDiagonalDown).
  151. Color = Word.WdColor.wdColorAutomatic;
  152. //第一行显示
  153. wordDoc.Tables.Item(1).Cell(1,2).
  154. Merge(wordDoc.Tables.Item(1).Cell(1,3));
  155. wordDoc.Tables.Item(1).Cell(1,4).
  156. Merge(wordDoc.Tables.Item(1).Cell(1,5));
  157. //第二行显示
  158. wordDoc.Tables.Item(1).Cell(2,5).
  159. Merge(wordDoc.Tables.Item(1).Cell(2,6));
  160. wordDoc.Tables.Item(1).Cell(1,4).
  161. Merge(wordDoc.Tables.Item(1).Cell(2,5));
  162. #region 插入数据行
  163. wordDoc.Tables.Item(1).Cell(1, 1).Range.Text = "cell11";
  164. //wordDoc.Tables.Item(k).Cell(1, 2).Range.Text =
  165. DT.Rows[i]["cell11"].ToString();
  166. ******************
  167. wordDoc.Tables.Item(1).Cell(1, 3).Range.Text = "cell13";
  168. //wordDoc.Tables.Item(k).Cell(1, 4).Range.Text =
  169. DT.Rows[i]["cell13"].ToString();
  170. ******************
  171. wordDoc.Tables.Item(1).Cell(2, 1).Range.Text = "cell21";
  172. //wordDoc.Tables.Item(k).Cell(2, 2).Range.Text =
  173. DT.Rows[i]["cell21"].ToString();
  174. ******************
  175. wordDoc.Tables.Item(1).Cell(2, 3).Range.Text = "cell23";
  176. //wordDoc.Tables.Item(k).Cell(2, 4).Range.Text =
  177. DT.Rows[i]["cell23"].ToString();
  178. #endregion
  179. //C#操作Word表格
  180. #region 第三行显示
  181. wordDoc.Tables.Item(1).Cell(3,2).
  182. Merge(wordDoc.Tables.Item(1).Cell(3,3));
  183. wordDoc.Tables.Item(1).Cell(3,2).
  184. Merge(wordDoc.Tables.Item(1).Cell(3,3));
  185. ******************
  186. wordDoc.Tables.Item(1).Cell(3, 1).Range.Text = "cell31";
  187. ******************
  188. wordDoc.Tables.Item(1).Cell(3, 3).Range.Text = "cell33";
  189. #endregion
  190. #region 第五行显示
  191. wordDoc.Tables.Item(1).Cell(5,2).
  192. Merge(wordDoc.Tables.Item(1).Cell(5,3));
  193. wordDoc.Tables.Item(1).Cell(5,2).
  194. Merge(wordDoc.Tables.Item(1).Cell(5,3));
  195. wordDoc.Tables.Item(1).Cell(5,2).
  196. Merge(wordDoc.Tables.Item(1).Cell(5,3));
  197. wordDoc.Tables.Item(1).Cell(5,2).
  198. Merge(wordDoc.Tables.Item(1).Cell(5,3));
  199. #endregion
  200. #region 第四行显示
  201. ******************
  202. wordDoc.Tables.Item(1).Cell(4, 1).Range.Text = "cell41";
  203. ******************
  204. wordDoc.Tables.Item(1).Cell(4, 3).Range.Text = "cell43";
  205. ******************
  206. wordDoc.Tables.Item(1).Cell(4, 5).Range.Text = "cell45";
  207. #endregion
  208. //C#操作Word表格
  209. #region 第六行显示
  210. wordDoc.Tables.Item(1).Cell(6,2).
  211. Merge(wordDoc.Tables.Item(1).Cell(6,3));
  212. wordDoc.Tables.Item(1).Cell(6,2).
  213. Merge(wordDoc.Tables.Item(1).Cell(6,3));
  214. wordDoc.Tables.Item(1).Cell(6,2).
  215. Merge(wordDoc.Tables.Item(1).Cell(6,3));
  216. wordDoc.Tables.Item(1).Cell(6,2).
  217. Merge(wordDoc.Tables.Item(1).Cell(6,3));
  218. ******************
  219. wordDoc.Tables.Item(1).Cell(5, 1).
  220. Range.Text = "cell51";
  221. wordDoc.Tables.Item(1).Cell(5, 2).
  222. Range.ParagraphFormat.Alignment =
  223. Word.WdParagraphAlignment.wdAlignParagraphLeft;
  224. ******************
  225. wordDoc.Tables.Item(1).Cell(6, 1).Range.Text = "cdll61";
  226. wordDoc.Tables.Item(1).Cell(6, 2).
  227. Range.ParagraphFormat.Alignment =
  228. Word.WdParagraphAlignment.wdAlignParagraphLeft;
  229. #endregion
  230. #region 第七行显示
  231. wordDoc.Tables.Item(1).Cell(7,2).
  232. Merge(wordDoc.Tables.Item(1).Cell(7,3));
  233. wordDoc.Tables.Item(1).Cell(7,2).
  234. Merge(wordDoc.Tables.Item(1).Cell(7,3));
  235. wordDoc.Tables.Item(1).Cell(7,2).
  236. Merge(wordDoc.Tables.Item(1).Cell(7,3));
  237. wordDoc.Tables.Item(1).Cell(7,2).
  238. Merge(wordDoc.Tables.Item(1).Cell(7,3));
  239. ******************
  240. wordDoc.Tables.Item(1).Cell(7, 1).Range.Text = "cell71";
  241. wordDoc.Tables.Item(1).Cell(7, 2).Range.
  242. ParagraphFormat.Alignment = Word.WdParagraphAlignment.
  243. wdAlignParagraphLeft;
  244. #endregion
  245. #region 第八行显示
  246. wordDoc.Tables.Item(1).Cell(8,1).
  247. Merge(wordDoc.Tables.Item(1).Cell(8,2));
  248. wordDoc.Tables.Item(1).Cell(8,2).
  249. Merge(wordDoc.Tables.Item(1).Cell(8,3));
  250. wordDoc.Tables.Item(1).Cell(8,2).
  251. Merge(wordDoc.Tables.Item(1).Cell(8,3));
  252. wordDoc.Tables.Item(1).Cell(8,2).
  253. Merge(wordDoc.Tables.Item(1).Cell(8,3));
  254. #endregion
  255. #region 第九行显示
  256. wordDoc.Tables.Item(1).Cell(9,1).Merge(wordDoc.Tables.Item(1).Cell(9,2));
  257. wordDoc.Tables.Item(1).Cell(9,3).Merge(wordDoc.Tables.Item(1).Cell(9,4));
  258. ******************
  259. wordDoc.Tables.Item(1).Cell(9, 1).Range.Text = "cell91";
  260. //wordDoc.Tables.Item(k).Cell(9, 2).Range.Text =
  261. (DT.Rows[i]["cell91"].ToString()=="1"?"有":"无");
  262. //C#操作Word表格
  263. ******************
  264. wordDoc.Tables.Item(1).Cell(9, 3).Range.Text = "cell93";
  265. #endregion
  266. #region 第十行显示
  267. wordDoc.Tables.Item(1).Cell(10,1).Merge(wordDoc.Tables.Item(1).Cell(10,2));
  268. wordDoc.Tables.Item(1).Cell(10,3).Merge(wordDoc.Tables.Item(1).Cell(10,4));
  269. ******************
  270. wordDoc.Tables.Item(1).Cell(10, 1).Range.Text = "cell101";
  271. ******************
  272. wordDoc.Tables.Item(1).Cell(10, 3).Range.Text = "cdll103";
  273. //wordDoc.Tables.Item(k).Cell(10, 4).Range.Text =
  274. (DT.Rows[i]["Label"].ToString()=="1"?"有":"无");
  275. #endregion
  276. #region 第十一行显示
  277. wordDoc.Tables.Item(1).Cell(11,1).
  278. Merge(wordDoc.Tables.Item(1).Cell(11,2));
  279. wordDoc.Tables.Item(1).Cell(11,3).
  280. Merge(wordDoc.Tables.Item(1).Cell(11,4));
  281. ******************
  282. wordDoc.Tables.Item(1).Cell(11, 1).Range.Text = "cell111";
  283. ******************
  284. wordDoc.Tables.Item(1).Cell(11, 3).Range.Text = "cell113";
  285. #endregion
  286. #region 第十二行显示
  287. wordDoc.Tables.Item(1).Cell(12,1).
  288. Merge(wordDoc.Tables.Item(1).Cell(12,2));
  289. wordDoc.Tables.Item(1).Cell(12,3).
  290. Merge(wordDoc.Tables.Item(1).Cell(12,4));
  291. ******************
  292. wordDoc.Tables.Item(1).Cell(12, 1).Range.Text = "cell121";
  293. ******************
  294. wordDoc.Tables.Item(1).Cell(12, 3).Range.Text = "cell123";
  295. #endregion
  296. #region 第十三行显示
  297. wordDoc.Tables.Item(1).Cell(13,1).
  298. Merge(wordDoc.Tables.Item(1).Cell(13,2));
  299. wordDoc.Tables.Item(1).Cell(13,3).
  300. Merge(wordDoc.Tables.Item(1).Cell(13,4));
  301. ******************
  302. wordDoc.Tables.Item(1).Cell(13, 1).Range.Text = "cell131";
  303. //C#操作Word表格
  304. ******************
  305. wordDoc.Tables.Item(1).Cell(13, 3).Range.Text = "cell133";
  306. #endregion
  307. wordDoc.Tables.Item(1).Select();
  308. wordApp.Application.Selection.Cut();
  309. //重新成声所有表
  310. for(int i = 0; i<=DT.Rows.Count-1;i++)
  311. {
  312. wordApp.Application.Selection.Paste();
  313. int k = i+1;
  314. #region 更新数据
  315. #region 插入数据行
  316. wordDoc.Tables.Item(k).Cell(1, 2).Range.Text =
  317. DT.Rows[i]["1"].ToString();
  318. ******************
  319. wordDoc.Tables.Item(k).Cell(1, 4).Range.Text =
  320. DT.Rows[i]["2"].ToString();
  321. ******************
  322. wordDoc.Tables.Item(k).Cell(2, 2).Range.Text =
  323. DT.Rows[i]["3"].ToString();
  324. ******************
  325. wordDoc.Tables.Item(k).Cell(2, 4).Range.Text =
  326. DT.Rows[i]["4"].ToString();
  327. #endregion
  328. //C#操作Word表格
  329. #region 第三行显示
  330. ******************
  331. wordDoc.Tables.Item(k).Cell(3, 2).Range.Text =
  332. DT.Rows[i]["5"].ToString();
  333. ******************
  334. wordDoc.Tables.Item(k).Cell(3, 4).Range.Text =
  335. DT.Rows[i]["6"].ToString();
  336. #endregion
  337. #region 第五行显示
  338. ******************
  339. wordDoc.Tables.Item(k).Cell(5, 2).Range.Text = DT.Rows[i]["7"].ToString();
  340. wordDoc.Tables.Item(k).Cell(5, 2).Range.ParagraphFormat
  341. .Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
  342. #endregion
  343. #region 第四行显示
  344. ******************
  345. wordDoc.Tables.Item(k).Cell(4, 2).Range.Text = DT.Rows[i]["8"].ToString();
  346. ******************
  347. wordDoc.Tables.Item(k).Cell(4, 4).Range.Text = DT.Rows[i]["9"].ToString();
  348. ******************
  349. wordDoc.Tables.Item(k).Cell(4, 6).Range.Text = DT.Rows[i]["0"].ToString();
  350. #endregion
  351. #region 第六行显示
  352. ******************
  353. wordDoc.Tables.Item(k).Cell(6, 2).Range.Text =
  354. DT.Rows[i]["11"].ToString();
  355. wordDoc.Tables.Item(k).Cell(6, 2).Range.ParagraphFormat
  356. .Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
  357. #endregion
  358. //C#操作Word表格
  359. #region 第七行显示
  360. ******************
  361. wordDoc.Tables.Item(k).Cell(7, 2).Range.
  362. Text = DT.Rows[i]["12"].ToString();
  363. wordDoc.Tables.Item(k).Cell(7, 2).Range.
  364. ParagraphFormat.Alignment = Word.WdParagraphAlignment.
  365. wdAlignParagraphLeft;
  366. #endregion
  367. #region 第八行显示
  368. ******************
  369. string strTechlevel = DT.Rows[i]["Level"].ToString();
  370. string returnTechlevel = "";
  371. switch(strTechlevel)
  372. {
  373. case "1":
  374. returnTechlevel = "Level1";
  375. break;
  376. case "2":
  377. returnTechlevel = "Level2";
  378. break;
  379. case "3":
  380. returnTechlevel = "Level3";
  381. break;
  382. case "0":
  383. returnTechlevel = "Level4";
  384. break;
  385. default:
  386. returnTechlevel = "Level5";
  387. break;
  388. }
  389. wordDoc.Tables.Item(k).Cell(8, 2).
  390. Range.Text = returnTechlevel;
  391. #endregion
  392. #region 第九行显示
  393. ******************
  394. wordDoc.Tables.Item(k).Cell(9, 2).Range.Text =
  395. (DT.Rows[i]["14"].ToString()=="1"?"有":"无");
  396. //C#操作Word表格
  397. ******************
  398. wordDoc.Tables.Item(k).Cell(9, 4).Range.Text =
  399. (DT.Rows[i]["15"].ToString()=="1"?"是":"否");
  400. #endregion
  401. #region 第十行显示
  402. ******************
  403. wordDoc.Tables.Item(k).Cell(10, 2).Range.Text =
  404. (DT.Rows[i]["16"].ToString()=="1"?"有":"无");
  405. ******************
  406. wordDoc.Tables.Item(k).Cell(10, 4).Range.Text =
  407. (DT.Rows[i]["17"].ToString()=="1"?"有":"无");
  408. #endregion
  409. #region 第十一行显示
  410. ******************
  411. wordDoc.Tables.Item(k).Cell(11, 2).Range.Text =
  412. (DT.Rows[i]["18"].ToString()=="1"?"是":"否");
  413. ******************
  414. wordDoc.Tables.Item(k).Cell(11, 4).Range.Text =
  415. (DT.Rows[i]["19"].ToString()=="1"?"是":"否");
  416. #endregion
  417. #region 第十二行显示
  418. ******************
  419. wordDoc.Tables.Item(k).Cell(12, 2).Range.Text =
  420. (DT.Rows[i]["20"].ToString()=="1"?"是":"否");
  421. ******************
  422. wordDoc.Tables.Item(k).Cell(12, 4).Range.Text =
  423. (DT.Rows[i]["21"].ToString()=="1"?"是":"否");
  424. #endregion
  425. #region 第十三行显示
  426. wordDoc.Tables.Item(k).Cell(13, 2).Range.Text =
  427. (DT.Rows[i]["22"].ToString()=="1"?"是":"否");
  428. ******************
  429. wordDoc.Tables.Item(k).Cell(13, 4).Range.Text =
  430. (DT.Rows[i]["23"].ToString()=="1"?"是":"否");
  431. #endregion
  432. #endregion
  433. //插入分页
  434. if(i!=DT.Rows.Count-1)
  435. {
  436. object mymissing = System.Reflection.Missing.Value;
  437. object myunit = Word.WdUnits.wdStory;
  438. wordApp.Selection.EndKey(ref myunit,ref mymissing);
  439. object pBreak= (int)Word.WdBreakType.wdPageBreak;
  440. wordApp.Selection.InsertBreak( ref pBreak );
  441. }
  442. }
  443. wordDoc.SaveAs(ref filename,
  444. ref missing,ref missing, ref missing,
  445. ref missing,ref missing,ref missing,
  446. ref missing,ref missing,ref missing, ref missing);
  447. //C#操作Word表格
  448. wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
  449. wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  450. if ( wordDoc != null )
  451. {
  452. System.Runtime.InteropServices.Marshal.
  453. ReleaseComObject(wordDoc);
  454. wordDoc = null;
  455. }
  456. if ( wordApp != null )
  457. {
  458. System.Runtime.InteropServices.Marshal.
  459. ReleaseComObject(wordApp);
  460. wordApp = null;
  461. }
  462. GC.Collect();
  463. //KillWordProcess();
  464. string strUrl = "MakeWordFile.aspx?username="
  465. +Request.QueryString["username"].ToString();
  466. utility.ShowPopMessage("文档生成完毕!",strUrl);
  467. }
  468. else
  469. {
  470. utility.ShowPopMessage("无任何数据!");
  471. }
  472. #endregion
  473. }
  474. catch
  475. {
  476. wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
  477. wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  478. if ( wordDoc != null )
  479. {
  480. System.Runtime.InteropServices.Marshal.
  481. ReleaseComObject(wordDoc);
  482. wordDoc = null;
  483. }
  484. if ( wordApp != null )
  485. {
  486. System.Runtime.InteropServices.Marshal.
  487. ReleaseComObject(wordApp);
  488. wordApp = null;
  489. }
  490. GC.Collect();
  491. utility.ShowPopMessage("文档生成失败!");
  492. }
  493. }

C#操作Word表格的实现实例就向你介绍到这里,希望对你学习和了解C#操作Word表格有所帮助。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值