ExcelHelper.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.XSSF.UserModel;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. namespace NXWMS.Client.Code.Excel
  9. {
  10. public static class SDExcelHelper
  11. {
  12. public static DataTable GetDataTable(string filepath)
  13. {
  14. DataTable dt = new DataTable();
  15. if (filepath.Last() == 's')
  16. {
  17. dt = ExcelHelper.ExcelToTableForXLS(filepath);
  18. }
  19. else
  20. {
  21. dt = ExcelHelper.ExcelToTableForXLSX(filepath);
  22. }
  23. return dt;
  24. }
  25. public class ExcelHelper
  26. {
  27. #region Excel2007
  28. /// <summary>
  29. /// 将Excel文件中的数据读出到DataTable中(xlsx)
  30. /// </summary>
  31. /// <param name="file"></param>
  32. /// <returns></returns>
  33. public static DataTable ExcelToTableForXLSX(string file)
  34. {
  35. DataTable dt = new DataTable();
  36. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  37. {
  38. XSSFWorkbook xssfworkbook = new XSSFWorkbook(fs);
  39. ISheet sheet = xssfworkbook.GetSheetAt(0);
  40. //表头
  41. IRow header = sheet.GetRow(sheet.FirstRowNum);
  42. List<int> columns = new List<int>();
  43. for (int i = 0; i < header.LastCellNum; i++)
  44. {
  45. object obj = GetValueTypeForXLSX(header.GetCell(i) as XSSFCell);
  46. if (obj == null || obj.ToString() == string.Empty)
  47. {
  48. dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
  49. }
  50. else
  51. dt.Columns.Add(new DataColumn(obj.ToString()));
  52. columns.Add(i);
  53. }
  54. //数据
  55. for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
  56. {
  57. DataRow dr = dt.NewRow();
  58. bool hasValue = false;
  59. foreach (int j in columns)
  60. {
  61. if (sheet.GetRow(i) != null)
  62. {
  63. dr[j] = GetValueTypeForXLSX(sheet.GetRow(i).GetCell(j) as XSSFCell);
  64. if (dr[j] != null && dr[j].ToString() != string.Empty)
  65. {
  66. hasValue = true;
  67. }
  68. }
  69. }
  70. if (hasValue)
  71. {
  72. dt.Rows.Add(dr);
  73. }
  74. }
  75. }
  76. return dt;
  77. }
  78. /// <summary>
  79. /// 将DataTable数据导出到Excel文件中(xlsx)
  80. /// </summary>
  81. /// <param name="dt"></param>
  82. /// <param name="file"></param>
  83. public static void TableToExcelForXLSX(DataTable dt, string file)
  84. {
  85. XSSFWorkbook xssfworkbook = new XSSFWorkbook();
  86. ISheet sheet = xssfworkbook.CreateSheet("Test");
  87. //表头
  88. IRow row = sheet.CreateRow(0);
  89. for (int i = 0; i < dt.Columns.Count; i++)
  90. {
  91. ICell cell = row.CreateCell(i);
  92. cell.SetCellValue(dt.Columns[i].ColumnName);
  93. }
  94. //数据
  95. for (int i = 0; i < dt.Rows.Count; i++)
  96. {
  97. IRow row1 = sheet.CreateRow(i + 1);
  98. for (int j = 0; j < dt.Columns.Count; j++)
  99. {
  100. ICell cell = row1.CreateCell(j);
  101. cell.SetCellValue(dt.Rows[i][j].ToString());
  102. }
  103. }
  104. //转为字节数组
  105. MemoryStream stream = new MemoryStream();
  106. xssfworkbook.Write(stream);
  107. var buf = stream.ToArray();
  108. //保存为Excel文件
  109. using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
  110. {
  111. fs.Write(buf, 0, buf.Length);
  112. fs.Flush();
  113. }
  114. }
  115. /// <summary>
  116. /// 获取单元格类型(xlsx)
  117. /// </summary>
  118. /// <param name="cell"></param>
  119. /// <returns></returns>
  120. private static object GetValueTypeForXLSX(XSSFCell cell)
  121. {
  122. if (cell == null)
  123. return null;
  124. switch (cell.CellType)
  125. {
  126. case CellType.Blank: //BLANK:
  127. return null;
  128. case CellType.Boolean: //BOOLEAN:
  129. return cell.BooleanCellValue;
  130. case CellType.Numeric: //NUMERIC:
  131. return cell.NumericCellValue;
  132. case CellType.String: //STRING:
  133. return cell.StringCellValue;
  134. case CellType.Error: //ERROR:
  135. return cell.ErrorCellValue;
  136. case CellType.Formula: //FORMULA:
  137. if (cell.CachedFormulaResultType == CellType.Numeric)
  138. {
  139. return cell.NumericCellValue;
  140. }
  141. else
  142. {
  143. return cell.StringCellValue;
  144. }
  145. default:
  146. return "=" + cell.CellFormula;
  147. }
  148. }
  149. #endregion
  150. #region Excel2003
  151. public static DataTable ExcelToTableForXLS(string file)
  152. {
  153. DataTable dt = new DataTable();
  154. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  155. {
  156. HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
  157. ISheet sheet = hssfworkbook.GetSheetAt(0);
  158. //表头
  159. IRow header = sheet.GetRow(sheet.FirstRowNum);
  160. List<int> columns = new List<int>();
  161. for (int i = 0; i < header.LastCellNum; i++)
  162. {
  163. object obj = GetValueTypeForXLS(header.GetCell(i) as HSSFCell);
  164. if (obj == null || obj.ToString() == string.Empty)
  165. {
  166. dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
  167. }
  168. else
  169. dt.Columns.Add(new DataColumn(obj.ToString()));
  170. columns.Add(i);
  171. }
  172. //数据
  173. for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
  174. {
  175. DataRow dr = dt.NewRow();
  176. bool hasValue = false;
  177. foreach (int j in columns)
  178. {
  179. if (sheet.GetRow(i) != null)
  180. {
  181. dr[j] = GetValueTypeForXLS(sheet.GetRow(i).GetCell(j) as HSSFCell);
  182. if (dr[j] != null && dr[j].ToString() != string.Empty)
  183. {
  184. hasValue = true;
  185. }
  186. }
  187. }
  188. if (hasValue)
  189. {
  190. dt.Rows.Add(dr);
  191. }
  192. }
  193. }
  194. return dt;
  195. }
  196. private static object GetValueTypeForXLS(HSSFCell cell)
  197. {
  198. if (cell == null)
  199. return null;
  200. switch (cell.CellType)
  201. {
  202. case CellType.Blank: //BLANK:
  203. return null;
  204. case CellType.Boolean: //BOOLEAN:
  205. return cell.BooleanCellValue;
  206. case CellType.Numeric: //NUMERIC:
  207. return cell.NumericCellValue;
  208. case CellType.String: //STRING:
  209. return cell.StringCellValue;
  210. case CellType.Error: //ERROR:
  211. return cell.ErrorCellValue;
  212. case CellType.Formula: //FORMULA:
  213. if (cell.CachedFormulaResultType == CellType.Numeric)
  214. {
  215. return cell.NumericCellValue;
  216. }
  217. else
  218. {
  219. return cell.StringCellValue;
  220. }
  221. default:
  222. return "=" + cell.CellFormula;
  223. }
  224. }
  225. #endregion
  226. }
  227. }
  228. }