C# 操作Excel实例浅析

开发 后端
C# 操作Excel实例主要是向你介绍了C# 操作Excel多个sheet的具体的操作,希望对你学习C# 操作Excel有所帮助。

C# 操作Excel多个sheet的具体的操作是什么呢?让我们来看看下面的实例实现:

  1. private void DataViewExcelBySheetMultipleDt(  
  2. DataSet ds, string fileName)     
  3. {     
  4. try    
  5. {     
  6. int sheetCount = ds.Tables.Count;     
  7. GC.Collect();     
  8. Application excel;     
  9. _Workbook xBk;     
  10. _Worksheet xSt = null;     
  11. excel = new ApplicationClass();     
  12. xBk = excel.Workbooks.Add(true);     
  13.     
  14. int rowIndex = 0;     
  15. int colIndex = 0;     
  16. for (int sheetIndex = 0;   
  17. sheetIndex < sheetCount; sheetIndex++)     
  18. {     
  19. rowIndex = 1;     
  20. colIndex = 1;     
  21. xSt = (_Worksheet)xBk.Worksheets.Add(  
  22. Type.Missing, Type.Missing, 1, Type.Missing);     
  23. switch (sheetIndex)     
  24. {     
  25. case 0:     
  26. xSt.Name = "test1";     
  27. break;     
  28. case 1:     
  29. xSt.Name = "test2";     
  30. break;     
  31. case 2:     
  32. xSt.Name = "test3";     
  33. break;     
  34. case 3:     
  35. xSt.Name = "test4";     
  36. break;     
  37. }     //C# 操作Excel多个sheet的具体的操作
  38. foreach (DataColumn col in ds.Tables[sheetIndex].Columns)     
  39. {     
  40. xSt.get_Range(excel.Cells[rowIndex,   
  41. colIndex], excel.Cells[rowIndex,   
  42. colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;     
  43. xSt.get_Range(excel.Cells[rowIndex,   
  44. colIndex], excel.Cells[rowIndex,   
  45. colIndex]).Font.Bold = true;     
  46. excel.Cells[rowIndex, colIndex++] = col.ColumnName;     
  47. }     
  48. foreach (DataRow row in ds.  
  49. Tables[sheetIndex].Rows)     
  50. {     
  51. rowIndex++;     
  52. colIndex = 1;     
  53. foreach (DataColumn col in ds.  
  54. Tables[sheetIndex].Columns)     
  55. {     
  56. if (col.DataType == System.Type.GetType(  
  57. "System.DateTime"))     
  58. {     
  59. if (!"".Equals(row[col.ColumnName].ToString()))     
  60. excel.Cells[rowIndex, colIndex] =   
  61. (Convert.ToDateTime(row[col.ColumnName].  
  62. ToString())).ToString("MM/dd/yyyy");     
  63. else    
  64. excel.Cells[rowIndex, colIndex] = "";     
  65. }     
  66. else if (col.DataType == S  
  67. ystem.Type.GetType("System.String"))     
  68. {     
  69. excel.Cells[rowIndex, colIndex] = "'" +   
  70. row[col.ColumnName].ToString();     
  71. }     
  72. else    
  73. {     
  74. excel.Cells[rowIndex, colIndex] =   
  75. row[col.ColumnName].ToString();     
  76. }     
  77. colIndex++;     
  78. }     //C# 操作Excel多个sheet的具体的操作
  79. }     
  80. Range allDataWithTitleRange = xSt.get_Range(  
  81. excel.Cells[1, 1], excel.Cells[rowIndex, colIndex - 1]);     
  82. allDataWithTitleRange.Select();     
  83. allDataWithTitleRange.Columns.AutoFit();     
  84. allDataWithTitleRange.Borders.LineStyle = 1;     
  85. }     
  86. string exportDir = "~/Attachment/";     
  87. string absFileName = HttpContext.  
  88. Current.Server.MapPath(  
  89. System.IO.Path.Combine(exportDir, fileName));     
  90. xBk.SaveCopyAs(absFileName);     
  91. xBk.Close(falsenullnull);     
  92. excel.Quit();     
  93.     
  94. System.Runtime.InteropServices.  
  95. Marshal.ReleaseComObject(xBk);     
  96. System.Runtime.InteropServices.  
  97. Marshal.ReleaseComObject(excel);     
  98. System.Runtime.InteropServices.  
  99. Marshal.ReleaseComObject(xSt);     
  100.     //C# 操作Excel多个sheet的具体的操作
  101. xBk = null;     
  102. excel = null;     
  103. xSt = null;     
  104. GC.Collect();     
  105. }     
  106. catch (Exception ex)     
  107. {     
  108.     
  109. }     
  110. }    
  111. private void DataViewExcelBySheetMultipleDt(  
  112. DataSet ds, string fileName)  
  113. {  
  114. try 
  115. {  
  116. int sheetCount = ds.Tables.Count;  
  117. GC.Collect();  
  118. Application excel;  
  119. _Workbook xBk;  
  120. _Worksheet xSt = null;  
  121. excel = new ApplicationClass();  
  122. xBk = excel.Workbooks.Add(true);  
  123.  //C# 操作Excel多个sheet的具体的操作
  124. int rowIndex = 0;  
  125. int colIndex = 0;  
  126. for (int sheetIndex = 0;  
  127.  sheetIndex < sheetCount; sheetIndex++)  
  128. {  
  129. rowIndex = 1;  
  130. colIndex = 1;  
  131. xSt = (_Worksheet)xBk.Worksheets.Add(  
  132. Type.Missing, Type.Missing, 1, Type.Missing);  
  133. switch (sheetIndex)  
  134. {  
  135. case 0:  
  136. xSt.Name = "test1";  
  137. break;  
  138. case 1:  
  139. xSt.Name = "test2";  
  140. break;  
  141. case 2:  
  142. xSt.Name = "test3";  
  143. break;  
  144. case 3:  
  145. xSt.Name = "test4";  
  146. break;  
  147. }  
  148. foreach (DataColumn col in ds.Tables[sheetIndex].Columns)  
  149. {  //C# 操作Excel多个sheet的具体的操作
  150. xSt.get_Range(excel.Cells[rowIndex,   
  151. colIndex], excel.Cells[rowIndex,   
  152. colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;  
  153. xSt.get_Range(excel.Cells[rowIndex,   
  154. colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;  
  155. excel.Cells[rowIndex, colIndex++] = col.ColumnName;  
  156. }  
  157. foreach (DataRow row in ds.Tables[sheetIndex].Rows)  
  158. {  
  159. rowIndex++;  
  160. colIndex = 1;  
  161. foreach (DataColumn col in ds.Tables[  
  162. sheetIndex].Columns)  
  163. {  
  164. if (col.DataType == System.Type.GetType(  
  165. "System.DateTime"))  
  166. {  
  167. if (!"".Equals(row[col.ColumnName].ToString()))  
  168. excel.Cells[rowIndex, colIndex] = (  
  169. Convert.ToDateTime(row[col.ColumnName].  
  170. ToString())).ToString("MM/dd/yyyy");  
  171. else 
  172. excel.Cells[rowIndex, colIndex] = "";  
  173. }  
  174. else if (col.DataType ==   
  175. System.Type.GetType("System.String"))  
  176. {  
  177. excel.Cells[rowIndex,   
  178. colIndex] = "'" + row[col.ColumnName].ToString();  
  179. }  
  180. else 
  181. {  
  182. excel.Cells[rowIndex,  
  183.  colIndex] = row[col.ColumnName].ToString();  
  184. }  
  185. colIndex++;  
  186. }  
  187. }  //C# 操作Excel多个sheet的具体的操作
  188. Range allDataWithTitleRange = xSt.get_Range(  
  189. excel.Cells[1, 1],   
  190. excel.Cells[rowIndex, colIndex - 1]);  
  191. allDataWithTitleRange.Select();  
  192. allDataWithTitleRange.Columns.AutoFit();  
  193. allDataWithTitleRange.Borders.LineStyle = 1;  
  194. }  
  195. string exportDir = "~/Attachment/";  
  196. string absFileName = HttpContext.Current.Server.  
  197. MapPath(System.IO.Path.Combine(exportDir, fileName));  
  198. xBk.SaveCopyAs(absFileName);  
  199. xBk.Close(falsenullnull);  
  200. excel.Quit();  
  201.  
  202. System.Runtime.InteropServices.  
  203. Marshal.ReleaseComObject(xBk);  
  204. System.Runtime.InteropServices.  
  205. Marshal.ReleaseComObject(excel);  
  206. System.Runtime.InteropServices.  
  207. Marshal.ReleaseComObject(xSt);  
  208.  
  209. xBk = null;  
  210. excel = null;  
  211. xSt = null;  
  212. GC.Collect();  
  213. }  
  214. catch (Exception ex)  
  215. {  
  216.  
  217. }  
  218. }  

上面方法,首先形成一个多个DataTable的DataSet,

C# 操作Excel重点还是

1. 生成一个新的xls时,打开方式,总是会提示进程占用

2. 用不同的sheet时一定要命名

3. 使用传入一个datatable时,总是会重写第一个sheet

C# 操作Excel多个sheet的具体的操作实例就向你介绍到这里,希望对你了解和学习C# 操作Excel多个sheet的具体的操作有所帮助。

【编辑推荐】

  1. C#枚举类型学习之基础讲解
  2. C#枚举类型语法浅析
  3. C#枚举类型基础学习浅析
  4. C#枚举类型使用的一点总结
  5. C#枚举文件的代码实现
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-08-18 16:04:12

C# 操作Excel

2009-08-18 16:14:05

C# 操作Excel

2009-08-18 16:20:09

C# 操作Excel

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-31 18:38:59

C#写文件

2009-08-19 09:42:52

C#操作Word书签

2009-08-18 14:25:05

C# 操作Excel

2009-08-19 10:25:14

C#操作Word

2009-08-20 11:07:07

C#共享内存

2009-08-26 13:48:31

C#打印条码

2009-08-19 11:28:41

C#操作Word

2009-09-01 13:59:01

C#操作Excel

2009-08-27 13:30:11

C# interfac

2009-08-18 17:42:12

C#操作符重载

2009-08-19 16:30:55

C#操作Access数

2009-08-19 14:12:23

C#操作注册表

2009-08-18 15:31:07

C# 操作Excel

2009-08-19 17:44:15

C#操作文本文件

2009-08-18 16:49:05

C# 操作XML
点赞
收藏

51CTO技术栈公众号