POI和easyExcel

概述

常用场景

1.将用户信息导出为excel表格(导出数据)

2.将excel表中的信息录入到网站数据库(习题上传),大大减轻网站录入量!

开发中经常会涉及到excel的处理,如导出excel,导入excel到数据库中。

操作excel目前比较流行的就是Apache POI和阿里巴巴的 easyExcel.

Apache POI

会比较麻烦

官网:https://poi.apache.org/

image-20210922133812385.png

image-20210922133820264.png

easyExcel

image-20210922134128673.png

EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。

EasyExcel能大大减少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。

下图是EasyExcel和POI在解析Excel的对比图

1584449986458-75c79796-c987-4b0f-92cd-82cd1855384e.png

官方文档:https://www.yuque.com/easyexcel/doc/about

POI-Excel写

基本使用

导入依赖

  1. <!--导入依赖-->
  2. <dependencies>
  3. <!--java万物皆对象-->
  4. <!--xls(03)-->
  5. <dependency>
  6. <groupId>org.apache.poi</groupId>
  7. <artifactId>poi</artifactId>
  8. <version>4.1.2</version>
  9. </dependency>
  10. <!--xlsx(07)-->
  11. <dependency>
  12. <groupId>org.apache.poi</groupId>
  13. <artifactId>poi-ooxml</artifactId>
  14. <version>4.1.2</version>
  15. </dependency>
  16. <!--日期格式化工具-->
  17. <dependency>
  18. <groupId>joda-time</groupId>
  19. <artifactId>joda-time</artifactId>
  20. <version>2.10.1</version>
  21. </dependency>
  22. <!--test-->
  23. <dependency>
  24. <groupId>junit</groupId>
  25. <artifactId>junit</artifactId>
  26. <version>4.12</version>
  27. </dependency>
  28. </dependencies>

03版本

  1. @Test
  2. public void testWriter03() throws Exception {
  3. //1.创建一个工作簿 03
  4. Workbook workbook = new HSSFWorkbook();
  5. //2.创建一个工作表
  6. Sheet sheet = workbook.createSheet("观众统计表");
  7. //3.创建一个行 (1,1)
  8. Row row1 = sheet.createRow(0);
  9. //4.创建一个单元格
  10. Cell cell11 = row1.createCell(0);
  11. cell11.setCellValue("今日新增观众");
  12. //(1,2)
  13. Cell cell12 = row1.createCell(1);
  14. cell12.setCellValue(666);
  15. //第二行(2,1)
  16. Row row2 = sheet.createRow(1);
  17. Cell cell21 = row2.createCell(0);
  18. cell21.setCellValue("统计时间");
  19. //(2,2)
  20. Cell cell22 = row2.createCell(1);
  21. String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
  22. cell22.setCellValue(time);
  23. //生成一张表(IO流) 03版本就是使用xls结尾
  24. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表03.xls");
  25. workbook.write(fileOutputStream);
  26. //关闭流
  27. fileOutputStream.close();
  28. System.out.println("观众统计表03.xls生成完毕");
  29. }

07版本

  1. @Test
  2. public void testWriter07() throws Exception {
  3. //1.创建一个工作簿 07
  4. Workbook workbook = new XSSFWorkbook();
  5. //2.创建一个工作表
  6. Sheet sheet = workbook.createSheet("观众统计表");
  7. //3.创建一个行 (1,1)
  8. Row row1 = sheet.createRow(0);
  9. //4.创建一个单元格
  10. Cell cell11 = row1.createCell(0);
  11. cell11.setCellValue("今日新增观众");
  12. //(1,2)
  13. Cell cell12 = row1.createCell(1);
  14. cell12.setCellValue(666);
  15. //第二行(2,1)
  16. Row row2 = sheet.createRow(1);
  17. Cell cell21 = row2.createCell(0);
  18. cell21.setCellValue("统计时间");
  19. //(2,2)
  20. Cell cell22 = row2.createCell(1);
  21. String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
  22. cell22.setCellValue(time);
  23. //生成一张表(IO流) 03版本就是使用xls结尾
  24. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表07.xlsx");
  25. workbook.write(fileOutputStream);
  26. //关闭流
  27. fileOutputStream.close();
  28. System.out.println("观众统计表07.xlsX生成完毕");
  29. }

注意一个对象的区别,文件后缀!

数据批量导入

大文件写HSSF(03)

缺点:最多只能处理65536行,否则抛出异常

优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快

  1. @Test
  2. public void testWriterBigData03() throws Exception {
  3. //时间
  4. long begin = System.currentTimeMillis();
  5. //创建一个簿
  6. Workbook workbook = new HSSFWorkbook();
  7. //创建表
  8. Sheet sheet = workbook.createSheet();
  9. //写入数据
  10. for (int rowNumber = 0; rowNumber < 65536; rowNumber++) {
  11. Row row = sheet.createRow(rowNumber);
  12. for (int cellNum = 0; cellNum < 10; cellNum++) {
  13. Cell cell = row.createCell(cellNum);
  14. cell.setCellValue(cellNum);
  15. }
  16. }
  17. System.out.println("over");
  18. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWriterBigData03.xls");
  19. workbook.write(fileOutputStream);
  20. fileOutputStream.close();
  21. long end = System.currentTimeMillis();
  22. System.out.println((double) (end - begin) / 1000);
  23. }

大文件写XSSF(07)

缺点:写数据时操作非常慢,非常耗内存,也会发生内存溢出,如100万条

优点:可以写较大的数据量,如20万条

  1. @Test
  2. public void testWriterBigData07() throws Exception {
  3. //时间
  4. long begin = System.currentTimeMillis();
  5. //创建一个簿
  6. Workbook workbook = new XSSFWorkbook();
  7. //创建表
  8. Sheet sheet = workbook.createSheet();
  9. //写入数据
  10. for (int rowNumber = 0; rowNumber < 65537; rowNumber++) {
  11. Row row = sheet.createRow(rowNumber);
  12. for (int cellNum = 0; cellNum < 10; cellNum++) {
  13. Cell cell = row.createCell(cellNum);
  14. cell.setCellValue(cellNum);
  15. }
  16. }
  17. System.out.println("over");
  18. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWriterBigData07.xlsx");
  19. workbook.write(fileOutputStream);
  20. fileOutputStream.close();
  21. long end = System.currentTimeMillis();
  22. System.out.println((double) (end - begin) / 1000);
  23. }

大文件写SXSSF

优点:可以写非常大的数据量,如100万条甚至更多,写数据速度快,占用更少的内存

  1. @Test
  2. public void testWriterBigData07S() throws Exception {
  3. //时间
  4. long begin = System.currentTimeMillis();
  5. //创建一个簿
  6. Workbook workbook = new SXSSFWorkbook();
  7. //创建表
  8. Sheet sheet = workbook.createSheet();
  9. //写入数据
  10. for (int rowNumber = 0; rowNumber < 65537; rowNumber++) {
  11. Row row = sheet.createRow(rowNumber);
  12. for (int cellNum = 0; cellNum < 10; cellNum++) {
  13. Cell cell = row.createCell(cellNum);
  14. cell.setCellValue(cellNum);
  15. }
  16. }
  17. System.out.println("over");
  18. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWriterBigData07S.xlsx");
  19. workbook.write(fileOutputStream);
  20. fileOutputStream.close();
  21. //清除临时文件!
  22. ((SXSSFWorkbook)workbook).dispose();
  23. long end = System.currentTimeMillis();
  24. System.out.println((double) (end - begin) / 1000);
  25. }

注意:

  1. 过程中会产生临时文件,需要清理临时文件
  2. 默认由100条记录被保存在内存中,如果超过这数量,则最前面的数据被写入临时文件
  3. 如果想自定义内存中数据的数量,可以使用 new SXSSFWorkBook(数量)

SXSSFWorkbook来自官方的解释:实现“BigGridDemo”策略的流式XSSFWorkbook版本。这允许写入非常大的文件而不会耗尽内存,因为任何时候只有可配置的行部分被保存在内存中。

请注意,仍然可能会消耗大量内存,这些内存基于您正在使用的功能,例如合并区域,注释……仍然只存储在内存中,因此如果广泛使用,可能需要大量内存。

POI-Excel读

03 | 07

03版本

  1. @Test
  2. public void testRead03() throws Exception {
  3. //获取文件流
  4. FileInputStream fileInputStream = new FileInputStream(PATH + "观众统计表03.xls");
  5. //1.创建一个工作簿。 使用excel能操作的这边他都可以操作
  6. Workbook workbook = new HSSFWorkbook(fileInputStream);
  7. //2.得到表
  8. Sheet sheet = workbook.getSheetAt(0);
  9. //3.得到行
  10. Row row = sheet.getRow(0);
  11. //4.得到列
  12. Cell cell = row.getCell(1);
  13. //读取值的时候,一定要注意类型
  14. //getStringCellValue 字符串类型
  15. // System.out.println(cell.getStringCellValue());
  16. System.out.println(cell.getNumericCellValue());
  17. fileInputStream.close();
  18. }

07版本

  1. @Test
  2. public void testRead07() throws Exception {
  3. //获取文件流
  4. FileInputStream fileInputStream = new FileInputStream(PATH + "观众统计表07.xlsx");
  5. //1.创建一个工作簿。 使用excel能操作的这边他都可以操作
  6. Workbook workbook = new XSSFWorkbook(fileInputStream);
  7. //2.得到表
  8. Sheet sheet = workbook.getSheetAt(0);
  9. //3.得到行
  10. Row row = sheet.getRow(0);
  11. //4.得到列
  12. Cell cell = row.getCell(1);
  13. //读取值的时候,一定要注意类型
  14. //getStringCellValue 字符串类型
  15. // System.out.println(cell.getStringCellValue());
  16. System.out.println(cell.getNumericCellValue());
  17. fileInputStream.close();
  18. }

注意获取的类型即可

读取不同的数据类型(麻烦)

  1. @Test
  2. public void testCellType() throws Exception {
  3. //获取文件流
  4. FileInputStream fileInputStream = new FileInputStream(PATH + "明细表.xls");
  5. //1.创建一个工作簿。 使用excel能操作的这边他都可以操作
  6. Workbook workbook = new HSSFWorkbook(fileInputStream);
  7. //获取标题内容
  8. Sheet sheet = workbook.getSheetAt(0);
  9. Row rowTitle = sheet.getRow(0);
  10. if (rowTitle != null) {
  11. //一定要掌握
  12. int cellCount = rowTitle.getPhysicalNumberOfCells();
  13. for (int cellNum = 0; cellNum < cellCount; cellNum++) {
  14. Cell cell = rowTitle.getCell(cellNum);
  15. if (cell != null) {
  16. String cellValue = cell.getStringCellValue();
  17. System.out.print(cellValue + "|");
  18. }
  19. }
  20. System.out.println();
  21. }
  22. //获取表中的内容
  23. int rowCount = sheet.getPhysicalNumberOfRows();
  24. for (int rowNum = 1; rowNum < rowCount; rowNum++) {
  25. Row rowdata = sheet.getRow(rowNum);
  26. if (rowdata != null) {
  27. //读取列
  28. int cellCount = rowTitle.getPhysicalNumberOfCells();
  29. for (int cellNum = 0; cellNum < cellCount; cellNum++) {
  30. System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");
  31. Cell cell = rowdata.getCell(cellNum);
  32. //匹配列的数据类型
  33. if (cell != null) {
  34. CellType cellType = cell.getCellType();
  35. String cellValue = "";
  36. switch (cellType) {
  37. case STRING:
  38. System.out.println("String类型");
  39. cellValue = cell.getStringCellValue();
  40. break;
  41. case _NONE:
  42. System.out.println("_NONE类型");
  43. break;
  44. case NUMERIC://数组(日期、普通数字)
  45. System.out.println("NUMERIC类型");
  46. if (DateUtil.isCellDateFormatted(cell)) {//日期
  47. System.out.println("日期");
  48. Date date = cell.getDateCellValue();
  49. cellValue = new DateTime(date).toString("yyyy-MM-dd");
  50. } else {
  51. // 是数字的转换成字符串获取,防止数字过长
  52. System.out.println("转换为字符串");
  53. HSSFDataFormatter hssfDataFormat = new HSSFDataFormatter();
  54. cellValue = hssfDataFormat.formatCellValue(cell);
  55. }
  56. break;
  57. case BLANK:
  58. System.out.println("BLANK类型");
  59. break;
  60. case ERROR:
  61. System.out.println("数据类型错误");
  62. break;
  63. }
  64. System.out.println(cellValue);
  65. }
  66. }
  67. }
  68. }
  69. fileInputStream.close();
  70. }
  71. }

注意类型转换问题

计算公式

了解即可

  1. @Test
  2. public void testFormula() throws Exception {
  3. //获取文件流
  4. FileInputStream fileInputStream = new FileInputStream(PATH + "公式.xls");
  5. //1.创建一个工作簿。 使用excel能操作的这边他都可以操作
  6. Workbook workbook = new HSSFWorkbook(fileInputStream);
  7. Sheet sheet = workbook.getSheetAt(0);
  8. Row row = sheet.getRow(4);
  9. Cell cell = row.getCell(0);
  10. //拿到计算公式 eval
  11. FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
  12. //输出单元格的内容
  13. CellType cellType = cell.getCellType();
  14. switch (cellType) {
  15. case FORMULA:
  16. String formula = cell.getCellFormula();
  17. System.out.println(formula);
  18. //计算
  19. CellValue evalute = formulaEvaluator.evaluate(cell);
  20. String cellValue = evalute.formatAsString();
  21. System.out.println(cellValue);
  22. break;
  23. }
  24. fileInputStream.close();
  25. }

EasyExcel操作

导入依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>3.0.0-beta3</version>
  5. </dependency>

写入测试

https://www.yuque.com/easyexcel/doc/write

读取测试

https://www.yuque.com/easyexcel/doc/read

固定套路:

1、写入,固定类格式进行写入

2、读取,根据监听器设置的规则进行读取!