ExcelUtil.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.XSSF.UserModel;
  4. using System.IO;
  5. namespace YBEE.EQM.Core;
  6. /// <summary>
  7. /// Excel操作工具
  8. /// </summary>
  9. public static class ExcelUtil
  10. {
  11. /// <summary>
  12. /// 获取 Workbook 实例
  13. /// </summary>
  14. /// <param name="filePath"></param>
  15. /// <param name="fileStream"></param>
  16. /// <returns></returns>
  17. public static IWorkbook GetWorkbook(string filePath, FileStream fileStream)
  18. {
  19. string fileExt = Path.GetExtension(filePath).ToLower();
  20. if (fileExt == ".xls")
  21. {
  22. return new HSSFWorkbook(fileStream);
  23. }
  24. else
  25. {
  26. return new XSSFWorkbook(fileStream);
  27. }
  28. }
  29. /// <summary>
  30. /// 创建 Workbook
  31. /// </summary>
  32. /// <param name="filePath"></param>
  33. /// <returns></returns>
  34. public static IWorkbook GetWorkbook(string filePath)
  35. {
  36. string fileExt = Path.GetExtension(filePath).ToLower();
  37. if (fileExt == ".xls")
  38. {
  39. return new HSSFWorkbook();
  40. }
  41. else
  42. {
  43. return new XSSFWorkbook();
  44. }
  45. }
  46. }