ExcelHandler.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. sheet.SetColumnWidth(i, dataLength);
  76. }
  77. ICellStyle style = book.CreateCellStyle();
  78. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  79. style.FillPattern = FillPatternType.BIG_SPOTS;
  80. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  81. //设置边框
  82. style.BorderBottom = BorderStyle.THICK;
  83. style.BorderLeft = BorderStyle.THICK;
  84. style.BorderRight = BorderStyle.THICK;
  85. style.BorderTop = BorderStyle.THICK;
  86. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  87. int rowindex = 0;
  88. if (Captioon.Rows[0]["es_engenable"].ToString() == "-1")
  89. {
  90. IRow row0 = sheet.CreateRow(rowindex);
  91. for (int i = 0; i < Captioon.Rows.Count; i++)
  92. {
  93. row0.CreateCell(i).SetCellValue(Captioon.Rows[i]["esd_engcaption"].ToString());
  94. row0.Cells[i].CellStyle = style;
  95. row0.Cells[i].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  96. row0.Cells[i].CellStyle.Alignment = HorizontalAlignment.CENTER;
  97. }
  98. rowindex = rowindex + 1;
  99. }
  100. if (Captioon.Rows[0]["es_chineseenable"].ToString() == "-1")
  101. {
  102. IRow row1 = sheet.CreateRow(rowindex);
  103. for (int i = 0; i < Captioon.Rows.Count; i++)
  104. {
  105. row1.CreateCell(i).SetCellValue(Captioon.Rows[i]["esd_caption"].ToString());
  106. row1.Cells[i].CellStyle = style;
  107. row1.Cells[i].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  108. row1.Cells[i].CellStyle.Alignment = HorizontalAlignment.CENTER;
  109. }
  110. rowindex = rowindex + 1;
  111. }
  112. //冻结第一行
  113. sheet.CreateFreezePane(0, rowindex, 0, rowindex);
  114. IRow row = sheet.CreateRow(rowindex);
  115. row.HeightInPoints = 20;
  116. //将DataTable的值循环赋值给book,Aligment设置居中
  117. //之前已经画了带颜色的第一行,所以从i=1开始画
  118. for (int i = 0; i < rowNum; i++)
  119. {
  120. row = sheet.CreateRow(i + rowindex);
  121. row.HeightInPoints = 20;
  122. for (int j = 0; j < columnNum; j++)
  123. {
  124. row.CreateCell(j);
  125. row.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  126. row.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  127. }
  128. }
  129. for (int i = 0; i < DataTable.Columns.Count; i++)
  130. {
  131. sheet.AutoSizeColumn(i);
  132. }
  133. //将book的内容写入内存流中返回
  134. book.Write(ms);
  135. return ms;
  136. }
  137. }
  138. }