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

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

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

【编辑推荐】

  1. C#操作符重载的那点事儿
  2. C#操作符重载特点实例浅析
  3. C#操作Word书签实例浅析
  4. C#操作Word表的实例浅析
  5. C#操作Word表格的常见操作
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-19 10:42:08

C#操作Word表格

2009-08-19 10:25:14

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-19 09:42:52

C#操作Word书签

2009-08-19 11:28:41

C#操作Word

2009-09-01 11:25:08

C#读取Word文件

2009-09-01 13:51:51

C#创建Word文档

2009-09-01 13:13:28

C#打开Word文档

2009-08-19 10:16:15

C#操作Word

2009-08-28 17:34:14

读取word文档

2009-09-01 11:21:02

C#读取word内容

2009-08-18 13:49:21

C# 操作Excel

2009-08-20 11:01:51

C#操作内存

2009-08-19 16:09:15

C#操作Access

2009-08-19 11:23:12

C#操作Word

2009-08-26 13:48:31

C#打印条码

2009-08-31 18:38:59

C#写文件

2009-08-18 17:05:08

C#操作xml文件

2009-08-18 16:04:12

C# 操作Excel
点赞
收藏

51CTO技术栈公众号