ExcelHandler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, string FolderPath, string FileName)
  16. {
  17. //创建一个内存流,用来接收转换成Excel的内容
  18. MemoryStream ms;
  19. ms = DataTableToExcel(dt);
  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)
  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. //首先画好第一行带颜色的,单独写出来,避免写在循环里面
  78. IRow row = sheet.CreateRow(0);
  79. //冻结第一行
  80. sheet.CreateFreezePane(0, 1, 0, 1);
  81. ICellStyle style = book.CreateCellStyle();
  82. style.FillForegroundColor = HSSFColor.PALE_BLUE.index;
  83. style.FillPattern = FillPatternType.BIG_SPOTS;
  84. style.FillBackgroundColor = HSSFColor.LIGHT_GREEN.index;
  85. //设置边框
  86. style.BorderBottom = BorderStyle.THICK;
  87. style.BorderLeft = BorderStyle.THICK;
  88. style.BorderRight = BorderStyle.THICK;
  89. style.BorderTop = BorderStyle.THICK;
  90. row.HeightInPoints = 20;
  91. //固定第一行
  92. //row.RowStyle.IsLocked=true;
  93. //给第一行的标签赋值样式和值
  94. for (int j = 0; j < columnNum; j++)
  95. {
  96. row.CreateCell(j);
  97. row.Cells[j].CellStyle = style;
  98. row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  99. row.Cells[j].CellStyle.Alignment = HorizontalAlignment.CENTER;
  100. row.Cells[j].SetCellValue(DataTable.Columns[j].ColumnName);
  101. }
  102. //将DataTable的值循环赋值给book,Aligment设置居中
  103. //之前已经画了带颜色的第一行,所以从i=1开始画
  104. for (int i = 0; i < rowNum; i++)
  105. {
  106. IRow row1 = sheet.CreateRow(i + 1);
  107. row1.HeightInPoints = 20;
  108. for (int j = 0; j < columnNum; j++)
  109. {
  110. row1.CreateCell(j);
  111. row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
  112. row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.CENTER;
  113. }
  114. }
  115. //将book的内容写入内存流中返回
  116. book.Write(ms);
  117. return ms;
  118. }
  119. }
  120. }