ExcelHandler.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.IO;
  2. using System.Data;
  3. using System;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.HSSF.Util;
  7. namespace UAS_LabelMachine
  8. {
  9. class ExcelHandler
  10. {
  11. DataHelper dh = new DataHelper();
  12. /// <summary>
  13. /// 导出Excel,返回文件在客户端的路径
  14. /// </summary>
  15. public string ExportExcel(DataTable dt, DataTable Captioon, string FolderPath, string FileName)
  16. {
  17. //创建一个内存流,用来接收转换成Excel的内容
  18. MemoryStream ms;
  19. ms = DataTableToExcel(dt, Captioon);
  20. //以系统当前时间命名文件,FileMode.Create表示创建文件,FileAccess.Write表示拥有写的权限
  21. string filePath = @FolderPath + "\\" + FileName + ".xls";
  22. FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  23. byte[] data = ms.ToArray();
  24. fs.Write(data, 0, data.Length);
  25. fs.Flush();
  26. //释放当前Excel文件,否则打开文件的时候会显示文件被占用
  27. ms.Dispose();
  28. fs.Dispose();
  29. return filePath;
  30. }
  31. /// <summary>
  32. /// 导入Excel
  33. /// </summary>
  34. public void ImportExcel(DataTable DataTable, string TableName)
  35. {
  36. int columnNum = DataTable.Columns.Count;
  37. int rowNum = DataTable.Columns.Count;
  38. string[] field = new string[columnNum];
  39. for (int i = 0; i < columnNum; i++)
  40. {
  41. field[i] = DataTable.Rows[0][i].ToString();
  42. }
  43. }
  44. /// <summary>
  45. /// 将DataTable形式的数据转成Excel格式的,然后用字节流的形式写入文件
  46. /// </summary>
  47. /// <param name="DataTable"></param>
  48. /// <returns></returns>
  49. public MemoryStream DataTableToExcel(DataTable DataTable, DataTable Captioon)
  50. {
  51. //创建内存流
  52. MemoryStream ms = new MemoryStream();
  53. //创建一个Book,相当于一个Excel文件
  54. HSSFWorkbook book = new HSSFWorkbook();
  55. //Excel中的Sheet
  56. ISheet sheet = book.CreateSheet("sheet1");
  57. //获取行数量和列数量
  58. int rowNum = DataTable.Rows.Count;
  59. int columnNum = DataTable.Columns.Count;
  60. //设置列的宽度,根据首行的列的内容的长度来设置
  61. for (int i = 0; i < columnNum; i++)
  62. {
  63. int dataLength;
  64. //如果内容比标题短则取标题长度
  65. if (DataTable.Rows[0][i].ToString().Length < DataTable.Columns[i].ColumnName.Length)
  66. {
  67. dataLength = DataTable.Columns[i].ColumnName.Length;
  68. dataLength = dataLength * 600;
  69. }
  70. else
  71. {
  72. dataLength = DataTable.Rows[0][i].ToString().Length;
  73. dataLength = dataLength * 500;
  74. }
  75. if (dataLength > 50000)
  76. dataLength = 50000;
  77. sheet.SetColumnWidth(i, dataLength);
  78. }
  79. ICellStyle style = book.CreateCellStyle();
  80. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  81. style.FillPattern = FillPatternType.BIG_SPOTS;
  82. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  83. //设置边框
  84. style.BorderBottom = BorderStyle.THICK;
  85. style.BorderLeft = BorderStyle.THICK;
  86. style.BorderRight = BorderStyle.THICK;
  87. style.BorderTop = BorderStyle.THICK;
  88. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  89. int rowindex = 0;
  90. if (Captioon.Rows[0]["es_engenable"].ToString() == "-1")
  91. {
  92. IRow row0 = sheet.CreateRow(rowindex);
  93. for (int i = 0; i < Captioon.Rows.Count; i++)
  94. {
  95. row0.CreateCell(i).SetCellValue(Captioon.Rows[i]["esd_engcaption"].ToString());
  96. row0.Cells[i].CellStyle = style;
  97. row0.Cells[i].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  98. row0.Cells[i].CellStyle.Alignment = HorizontalAlignment.CENTER;
  99. }
  100. rowindex = rowindex + 1;
  101. }
  102. if (Captioon.Rows[0]["es_chineseenable"].ToString() == "-1")
  103. {
  104. IRow row1 = sheet.CreateRow(rowindex);
  105. for (int i = 0; i < Captioon.Rows.Count; i++)
  106. {
  107. row1.CreateCell(i).SetCellValue(Captioon.Rows[i]["esd_caption"].ToString());
  108. row1.Cells[i].CellStyle = style;
  109. row1.Cells[i].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  110. row1.Cells[i].CellStyle.Alignment = HorizontalAlignment.CENTER;
  111. }
  112. rowindex = rowindex + 1;
  113. }
  114. //冻结第一行
  115. sheet.CreateFreezePane(0, rowindex, 0, rowindex);
  116. IRow row = sheet.CreateRow(rowindex);
  117. row.HeightInPoints = 20;
  118. //将DataTable的值循环赋值给book,Aligment设置居中
  119. //之前已经画了带颜色的第一行,所以从i=1开始画
  120. for (int i = 0; i < rowNum; i++)
  121. {
  122. row = sheet.CreateRow(i + rowindex);
  123. row.HeightInPoints = 20;
  124. for (int j = 0; j < columnNum; j++)
  125. {
  126. row.CreateCell(j);
  127. row.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  128. row.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  129. }
  130. }
  131. for (int i = 0; i < DataTable.Columns.Count; i++)
  132. {
  133. sheet.AutoSizeColumn(i);
  134. }
  135. //将book的内容写入内存流中返回
  136. book.Write(ms);
  137. return ms;
  138. }
  139. }
  140. }