ExPortToExcel.cs 8.8 KB

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