ExPortToExcel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using OfficeOpenXml;
  2. using System;
  3. using System.Collections;
  4. using System.Data;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8. namespace PTMedicalInsurance.Common
  9. {
  10. public class ExportToExcel
  11. {
  12. public Excel.Application m_xlApp = null;
  13. /// <summary>
  14. /// 将DataTable数据导出到Excel表
  15. /// </summary>
  16. /// <param name="tmpDataTable">要导出的DataTable</param>
  17. /// <param name="strFileName">Excel的保存路径及名称</param>
  18. public static string DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
  19. {
  20. if (tmpDataTable == null)
  21. {
  22. return "";
  23. }
  24. long rowNum = tmpDataTable.Rows.Count;//行数
  25. int columnNum = tmpDataTable.Columns.Count;//列数
  26. Excel.Application m_xlApp = new Excel.Application();
  27. m_xlApp.DisplayAlerts = false;//不显示更改提示
  28. m_xlApp.Visible = false;
  29. Excel.Workbooks workbooks = m_xlApp.Workbooks;
  30. Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
  31. Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
  32. try
  33. {
  34. if (rowNum > 65536)//单张Excel表格最大行数
  35. {
  36. long pageRows = 65535;//定义每页显示的行数,行数必须小于65536
  37. int scount = (int)(rowNum / pageRows);//导出数据生成的表单数
  38. if (scount * pageRows < rowNum)//当总行数不被pageRows整除时,经过四舍五入可能页数不准
  39. {
  40. scount = scount + 1;
  41. }
  42. for (int sc = 1; sc <= scount; sc++)
  43. {
  44. if (sc > 1)
  45. {
  46. object missing = System.Reflection.Missing.Value;
  47. worksheet = (Excel.Worksheet)workbook.Worksheets.Add(missing, missing, missing, missing);//添加一个sheet
  48. }
  49. else
  50. {
  51. worksheet = (Excel.Worksheet)workbook.Worksheets[sc];//取得sheet1
  52. }
  53. string[,] datas = new string[pageRows + 1, columnNum];
  54. for (int i = 0; i < columnNum; i++) //写入字段
  55. {
  56. datas[0, i] = tmpDataTable.Columns[i].Caption;//表头信息
  57. }
  58. Excel.Range range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, columnNum]];
  59. range.Interior.ColorIndex = 15;//15代表灰色
  60. range.Font.Bold = true;
  61. range.Font.Size = 9;
  62. int init = int.Parse(((sc - 1) * pageRows).ToString());
  63. int r = 0;
  64. int index = 0;
  65. int result;
  66. if (pageRows * sc >= rowNum)
  67. {
  68. result = (int)rowNum;
  69. }
  70. else
  71. {
  72. result = int.Parse((pageRows * sc).ToString());
  73. }
  74. for (r = init; r < result; r++)
  75. {
  76. index = index + 1;
  77. for (int i = 0; i < columnNum; i++)
  78. {
  79. object obj = tmpDataTable.Rows[r][tmpDataTable.Columns[i].ToString()];
  80. datas[index, i] = obj == null ? "" : "'" + obj.ToString().Trim();//在obj.ToString()前加单引号是为了防止自动转化格式
  81. }
  82. System.Windows.Forms.Application.DoEvents();
  83. //添加进度条
  84. }
  85. Excel.Range fchR = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[index + 1, columnNum]];
  86. fchR.Value2 = datas;
  87. worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
  88. m_xlApp.WindowState = Excel.XlWindowState.xlMaximized;//Sheet表最大化
  89. range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[index + 1, columnNum]];
  90. //range.Interior.ColorIndex = 15;//15代表灰色
  91. range.Font.Size = 9;
  92. range.RowHeight = 14.25;
  93. range.Borders.LineStyle = 1;
  94. range.HorizontalAlignment = 1;
  95. }
  96. }
  97. else
  98. {
  99. string[,] datas = new string[rowNum + 1, columnNum];
  100. for (int i = 0; i < columnNum; i++) //写入字段
  101. {
  102. //datas[0, i] = tmpDataTable.Columns[i].Caption;
  103. string ColumnsName = tmpDataTable.Columns[i].Caption;
  104. datas[0, i] = ColumnsName;
  105. //MessageBox.Show("哈哈:"+datas[0, i].ToString());
  106. }
  107. Excel.Range range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, columnNum]];
  108. range.Interior.ColorIndex = 15;//15代表灰色
  109. range.Font.Bold = true;
  110. range.Font.Size = 9;
  111. int r = 0;
  112. for (r = 0; r < rowNum; r++)
  113. {
  114. for (int i = 0; i < columnNum; i++)
  115. {
  116. object obj = tmpDataTable.Rows[r][tmpDataTable.Columns[i].ToString()];
  117. datas[r + 1, i] = obj == null ? "" : "'" + obj.ToString().Trim();//在obj.ToString()前加单引号是为了防止自动转化格式
  118. }
  119. System.Windows.Forms.Application.DoEvents();
  120. //添加进度条
  121. }
  122. Excel.Range fchR = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]];
  123. fchR.Value2 = datas;
  124. worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
  125. m_xlApp.WindowState = Excel.XlWindowState.xlMaximized;
  126. range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]];
  127. //range.Interior.ColorIndex = 15;//15代表灰色
  128. range.Font.Size = 9;
  129. range.RowHeight = 14.25;
  130. range.Borders.LineStyle = 1;
  131. range.HorizontalAlignment = 1;
  132. }
  133. workbook.Saved = true;
  134. workbook.SaveCopyAs(strFileName);
  135. m_xlApp.Workbooks.Close();
  136. m_xlApp.Workbooks.Application.Quit();
  137. m_xlApp.Application.Quit();
  138. m_xlApp.Quit();
  139. return strFileName;
  140. }
  141. catch (Exception ex)
  142. {
  143. MessageBox.Show("导出异常:" + ex.Message, "导出异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  144. return "";
  145. }
  146. finally
  147. {
  148. new ExportToExcel().EndReport();
  149. }
  150. }
  151. public static void ExportDataTableToExcel(DataTable table, string filePath)
  152. {
  153. using (var package = new ExcelPackage(new FileInfo(filePath)))
  154. {
  155. // 创建一个新的工作表
  156. var worksheet = package.Workbook.Worksheets.Add("Sheet1");
  157. // 写入列名
  158. for (int i = 0; i < table.Columns.Count; i++)
  159. {
  160. worksheet.Cells[1, i + 1].Value = table.Columns[i].ColumnName;
  161. }
  162. // 写入数据行
  163. for (int i = 0; i < table.Rows.Count; i++)
  164. {
  165. for (int j = 0; j < table.Columns.Count; j++)
  166. {
  167. worksheet.Cells[i + 2, j + 1].Value = table.Rows[i][j];
  168. }
  169. }
  170. // 保存文件
  171. package.Save();
  172. }
  173. }
  174. /// <summary>
  175. /// 退出报表时关闭Excel和清理垃圾Excel进程
  176. /// </summary>
  177. public void EndReport()
  178. {
  179. object missing = System.Reflection.Missing.Value;
  180. try
  181. {
  182. m_xlApp.Workbooks.Close();
  183. m_xlApp.Workbooks.Application.Quit();
  184. m_xlApp.Application.Quit();
  185. m_xlApp.Quit();
  186. }
  187. catch { }
  188. finally
  189. {
  190. try
  191. {
  192. System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp.Workbooks);
  193. System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp.Application);
  194. System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp);
  195. m_xlApp = null;
  196. }
  197. catch { }
  198. try
  199. {
  200. //清理垃圾进程
  201. this.killProcessThread();
  202. }
  203. catch { }
  204. GC.Collect();
  205. }
  206. }
  207. /// <summary>
  208. /// 杀掉不死进程
  209. /// </summary>
  210. public void killProcessThread()
  211. {
  212. ArrayList myProcess = new ArrayList();
  213. for (int i = 0; i < myProcess.Count; i++)
  214. {
  215. try
  216. {
  217. System.Diagnostics.Process.GetProcessById(int.Parse((string)myProcess[i])).Kill();
  218. }
  219. catch { }
  220. }
  221. }
  222. }
  223. }