智慧大脑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
2.0 KiB

  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using NPOI.XSSF.UserModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace DataSendApi.Program.BLL
  12. {
  13. /// <summary>
  14. /// Excel操作类
  15. /// </summary>
  16. public class HandleExcel
  17. {
  18. /// <summary>
  19. /// 读取ExcelFile
  20. /// </summary>
  21. /// <param name="filePath">文件地址</param>
  22. public List<List<string>> ReadFromExcelFile(string filePath)
  23. {
  24. IWorkbook wk = null;
  25. List<List<string>> returnLst = new List<List<string>>();
  26. string extension = Path.GetExtension(filePath);
  27. FileStream fs = File.OpenRead(filePath);
  28. if (extension.Equals(".xls"))
  29. {
  30. //把xls文件中的数据写入wk中
  31. wk = new HSSFWorkbook(fs);
  32. }
  33. else
  34. {
  35. //把xlsx文件中的数据写入wk中
  36. wk = new XSSFWorkbook(fs);
  37. }
  38. fs.Close();
  39. //读取当前表数据
  40. ISheet sheet = wk.GetSheetAt(0);
  41. IRow row = sheet.GetRow(0); //读取当前行数据
  42. for (int i = 0; i <= sheet.LastRowNum; i++)
  43. {
  44. row = sheet.GetRow(i); //读取当前行数据
  45. if (row != null)
  46. {
  47. List<string> _temp = new List<string>();
  48. //LastCellNum 是当前行的总列数
  49. for (int j = 0; j < row.LastCellNum; j++)
  50. {
  51. string value = string.Empty;
  52. if (row.GetCell(j) != null)
  53. //读取该行的第j列数据
  54. value = row.GetCell(j).ToString();
  55. _temp.Add(value);
  56. }
  57. returnLst.Add(_temp);
  58. }
  59. }
  60. return returnLst;
  61. }
  62. }
  63. }