日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#操作Excel實例淺析

C# 操作Excel多個sheet的具體的操作是什么呢?讓我們來看看下面的實例實現(xiàn):

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名申請、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、汝陽網(wǎng)站維護(hù)、網(wǎng)站推廣。

 
 
 
  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(false, null, null);   
  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(false, null, null);
  200. excel.Quit();
  201. System.Runtime.InteropServices.
  202. Marshal.ReleaseComObject(xBk);
  203. System.Runtime.InteropServices.
  204. Marshal.ReleaseComObject(excel);
  205. System.Runtime.InteropServices.
  206. Marshal.ReleaseComObject(xSt);
  207. xBk = null;
  208. excel = null;
  209. xSt = null;
  210. GC.Collect();
  211. }
  212. catch (Exception ex)
  213. {
  214. }

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

C# 操作Excel重點(diǎn)還是

1. 生成一個新的xls時,打開方式,總是會提示進(jìn)程占用

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

3. 使用傳入一個datatable時,總是會重寫第一個sheet

C# 操作Excel多個sheet的具體的操作實例就向你介紹到這里,希望對你了解和學(xué)習(xí)C# 操作Excel多個sheet的具體的操作有所幫助。


新聞名稱:C#操作Excel實例淺析
網(wǎng)頁URL:http://www.dlmjj.cn/article/djdssgd.html