ExPortToExcel.cs 8.9 KB

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