Excel

方式

1,POI方式

HSSFWorkbook用来操作Excel2003以前(包括2003)的版本,”.xls”行数最多65536行。
XSSFWorkbook用来操作Excel2007之后(包括2007)的版本,”.xlsx”行数最多1048576行。

2,EasyExcel方式

EasyExcel依赖POI版本最低为3.17;

POI 原理是将数据全部读取,放入内存中,容易内存溢出。
image.png
EasyExcel 原理是避免把数据全部放入内存中,并放弃一些不重要的样式。

3,HuTool方式

HuTool—4依赖POI版本最低为3.17; HuTool—5依赖POI版本最低为4.1.2; HuTool—5.6依赖POI版本最低为5;

写入

  1. package com.lgx.utils.excel;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class ExcelTest {
  6. public static void main(String[] args) throws IOException {
  7. UserPojo userPojo = new UserPojo();
  8. userPojo.setUserName("zs");
  9. userPojo.setPhone("123");
  10. UserPojo userPojo2 = new UserPojo();
  11. userPojo2.setUserName("ls");
  12. userPojo2.setPhone("321");
  13. List<UserPojo> userPojos = new ArrayList<UserPojo>();
  14. userPojos.add(userPojo);
  15. userPojos.add(userPojo2);
  16. String fileName = "/Users/liguoxi/tmp/aaa.xlsx";
  17. Poi.writeExcel(userPojos, fileName);
  18. EasyExcel.writeExcel(userPojos, fileName);
  19. HuTool.writeExcel(userPojos, fileName);
  20. }
  21. }
  1. package com.lgx.utils.excel;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import lombok.Data;
  4. @Data
  5. public class UserPojo {
  6. @ExcelProperty(index = 0, value = {"用户账号", "用户账号"})
  7. private String userName;//用户账号
  8. @ExcelProperty(index = 1, value = {"手机号码", "手机号码"})
  9. private String phone;//手机号码
  10. }

1,POI方式

maven引入依赖

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi</artifactId>
  4. <version>4.1.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi-ooxml</artifactId>
  9. <version>4.1.2</version>
  10. </dependency>

代码如下

  1. package com.lgx.utils.excel;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import lombok.Data;
  4. import org.apache.poi.ss.usermodel.Cell;
  5. import org.apache.poi.xssf.usermodel.XSSFCell;
  6. import org.apache.poi.xssf.usermodel.XSSFRow;
  7. import org.apache.poi.xssf.usermodel.XSSFSheet;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import java.io.*;
  10. import java.util.List;
  11. public class Poi {
  12. public static void writeExcel(List<UserPojo> userPojos, String fileName) throws IOException {
  13. //1,创建工作簿
  14. XSSFWorkbook workbook = new XSSFWorkbook();
  15. //2,创建工作表
  16. XSSFSheet sheet = workbook.createSheet("当前sheet页的名称");
  17. //取第二个sheet页
  18. //XSSFSheet sheet = workbook.getSheetAt(1);
  19. // 3,创建行,创建列
  20. XSSFRow row;
  21. XSSFCell cell;
  22. //4,创建第一行,标题行
  23. row = sheet.createRow(0);
  24. cell = row.createCell(0);
  25. cell.setCellValue("用户名称");
  26. cell = row.createCell(1);
  27. cell.setCellValue("手机号码");
  28. cell = row.createCell(2);
  29. //5,填充其余行,表数据
  30. for (int i = 0; i < userPojos.size(); i++) {
  31. UserPojo userPojo = userPojos.get(i);
  32. //第一行是表头 数据要从第二行开始
  33. row = sheet.createRow(i + 1);
  34. Cell cell2 = row.createCell(0);
  35. cell2.setCellValue(userPojo.getUserName());
  36. Cell cell5 = row.createCell(1);
  37. cell5.setCellValue(userPojo.getPhone());
  38. }
  39. //6,写入文件中
  40. FileOutputStream out = new FileOutputStream(new File(fileName));
  41. workbook.write(out);
  42. //7,关闭流
  43. out.close();
  44. }
  45. }

2,EasyExcel方式

maven引入依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>3.0.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.slf4j</groupId>
  8. <artifactId>slf4j-simple</artifactId>
  9. <version>1.7.25</version>
  10. </dependency>

代码如下

  1. package com.lgx.utils.excel;
  2. import java.util.List;
  3. public class EasyExcel {
  4. public static void writeExcel(List<UserPojo> userPojos, String fileName) {
  5. com.alibaba.excel.EasyExcel.write(fileName, UserPojo.class).sheet("当前sheet页的名称").doWrite(userPojos);
  6. }
  7. }

3,HuTool方式

  1. <dependency>
  2. <groupId>cn.hutool</groupId>
  3. <artifactId>hutool-all</artifactId>
  4. <version>5.8.0</version>
  5. </dependency>

代码如下所示

  1. package com.lgx.utils.excel;
  2. import cn.hutool.poi.excel.ExcelWriter;
  3. import java.util.List;
  4. public class HuTool {
  5. public static void writeExcel(List<UserPojo> userPojos, String fileName) {
  6. ExcelWriter writer = new ExcelWriter(fileName);
  7. //自定义标题别名
  8. writer.addHeaderAlias("userName", "用户账号");
  9. writer.addHeaderAlias("phone", "手机号码");
  10. // 一次性写出内容,使用默认样式,强制输出标题
  11. writer.write(userPojos, true);
  12. // 关闭writer,释放内存
  13. writer.close();
  14. }
  15. }