POI 与 easyExcel 操作 Excel
POI结构:
- HSSF : 提供读写Microsoft Excel格式档案的功能(2003)。
 
- XSSF : 提供读写Microsoft Excel OOXML格式档案的功能(2007)。
 
- HWPF : 提供读写Microsoft Word格式档案的功能。
 
- HSLF : 提供读写Microsoft PowerPoint格式档案的功能。
 
- HDGF : 提供读写Microsoft Visio格式档案的功能。
 
POI依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | <dependencies>          <dependency>         <groupId>org.apache.poi</groupId>         <artifactId>poi</artifactId>         <version>4.1.2</version>     </dependency>
           <dependency>         <groupId>org.apache.poi</groupId>         <artifactId>poi-ooxml</artifactId>         <version>4.1.2</version>     </dependency> </dependencies>
   | 
 
POI 写入 excel:
HSSFWorkbook(03):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | 
  Workbook workBook = new HSSFWorkbook();
  Sheet sheet = workBook.createSheet("sheet1");
  Row row1 = sheet.createRow(0);
  Cell cell1 = row1.createCell(0);
  cell1.setCellValue("(1,1)单元格");
 
  String path = "E:\\myfile\\IDEA\\demo"; String fileName = "excelTest.xls";  FileOutputStream excelFile = new FileOutputStream(new File(path,fileName));
  workBook.write(excelFile);
  excelFile.close();
 
  | 
 
XSSFWorkbook(07):
03与07只存在工作簿对象和文件后缀的不同,并不太大的改变。因为面向对象编程的好处我们只需修改接口对象与文件的后缀为xlsx即可完成03至07的转变。需改变内容如下:
1 2 3 4
   |  Workbook workBook = new XSSFWorkbook();
  String fileName = "excelTest.xlsx"; 
 
  | 
 
SXSSFWorkbook(07快速写入):
由上可发现 HSSFWorkbook 操作速度快但数量受限, XSSFWorkbook 数量没有限制但效力极低。
官方提供了另一个类 SXSSFWorkbook 来解决效率低的问题,它利用临时文件来存储数据。默认内存中最多保存100条数据超过时将前100条数据将写入临时文件中,也可在构造函数中传入自定义缓存数量。
注意: 产生的临时文件需要清理(dispose)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   |  Workbook workBook = new SXSSFWorkbook(); Sheet sheet1 = workBook.createSheet();
  for (int i = 0; i <65536 ; i++) {     Row row = sheet1.createRow(i);     for (int j = 0; j < 10; j++) {         Cell cell = row.createCell(j);         cell.setCellValue("("+i+","+j+")");     } } String path = "E:\\myfile\\IDEA\\demo"; String fileName = "sxssf.xlsx"; FileOutputStream excelFile = new FileOutputStream(new File(path, fileName));
  workBook.write(excelFile);
  excelFile.close();
  ((SXSSFWorkbook)workBook).dispose();
 
  | 
 
POI 读取 excel:
案例表格数据如下:(求和单元格公式为 =B2+C2 )
| 时间 | 
数字 | 
小数 | 
布尔 | 
字符串 | 
求和 | 
| 2021年6月10日 | 
999 | 
9.9 | 
TRUE | 
哈哈哈 | 
1008.9 | 
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
   | FileInputStream excelFile = new FileInputStream(new File(path, fileName));
  Workbook workBook = new HSSFWorkbook(excelFile);
  Sheet sheet = workBook.getSheetAt(0);
 
 
  Row rowTitle = sheet.getRow(0); if (rowTitle != null) {           int cellCount = rowTitle.getPhysicalNumberOfCells();     for (int cellNum = 0; cellNum < cellCount; cellNum++) {         Cell cell = rowTitle.getCell(cellNum);         if (cell != null) {                           String cellValue = cell.getStringCellValue();             System.out.print(cellValue+" | ");         }     }     System.out.println(); }
 
 
  FormulaEvaluator eval = new HSSFFormulaEvaluator((HSSFWorkbook) workBook);
  int rowCount = sheet.getPhysicalNumberOfRows(); for (int rowNum = 1; rowNum < rowCount; rowNum++) {     Row row = sheet.getRow(rowNum);     if (row != null) {         int cellCount = row.getPhysicalNumberOfCells();         for (int cellNum = 0; cellNum < cellCount; cellNum++) {             Cell cell = row.getCell(cellNum);             if (cell != null) {                 CellType cellType = cell.getCellType();                 String cellValue = "";                 switch (cellType) {                     case STRING:                          System.out.print("【字符串】:");                         cellValue = cell.getStringCellValue();                         break;                     case NUMERIC:                          System.out.print("【数值类型】:");                                                  if (DateUtil.isCellDateFormatted(cell)) {                             Date dateValue = cell.getDateCellValue();                             cellValue = new SimpleDateFormat("yyyy-MM-dd")                                     .format(dateValue);                         } else {                             cellValue = cell.toString();                         }                         break;                     case BOOLEAN:                          System.out.print("【布尔类型】:");                         cellValue = String.valueOf(cell.getBooleanCellValue());                         break;                     case FORMULA:                          System.out.print("【公式】:");                                                  String formula = cell.getCellFormula();                                                  CellValue value = eval.evaluate(cell);                                                  cellValue = value.formatAsString();                         break;                     case ERROR:                          System.out.print("【错误】:");                         break;                     case BLANK:                          System.out.print("【空】:");                         break;                     case _NONE:                          System.out.print("【未知】:");                         break;                 }                 System.out.println(cellValue);             }         }     } }
  excelFile.close();
   | 
 
运行结果:
1 2 3 4 5 6 7
   | 时间 | 数字 | 小数 | 布尔 | 字符串 | 求和 |  【数值类型】:2021-06-10 【数值类型】:999.0 【数值类型】:9.9 【布尔类型】:true 【字符串】:哈哈哈 【公式】:1008.9
   | 
 
easyExcel
依赖:
最新依赖获取地址
easyExcel 中关联了 POI 依赖因此我们导入 easyExcel 依赖后就不需要再到 POI 依赖也能使用 POI。不过注意版本上的区别,新版POI有许多新功能如需使用,建议还是添加最新版的POI依赖。
1 2 3 4 5
   | <dependency>   <groupId>com.alibaba</groupId>   <artifactId>easyexcel</artifactId>   <version>2.2.7</version> </dependency>
   | 
 
读写Excel:
在 EasyExcel 官网 有详细介绍这里不再做讲解。