高性能!POI与easyExcel 深入浅出
一、POI和easyExcel
- 运用场景:
- 导出数据为Excel
- 将Excel中的信息录入到数据库中
- 操作Excel流行的就是Apache POI 和 阿里巴巴的 easyExcel
- 什么是POI?
- POI是开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能
- 基本功能:
- 原生的相对来说比较复杂
- EasyExcel
- 参考地址:https://github.com/alibaba/easyexcel
- java解析Excel的工具,基于POI
- Excel不会再出现溢出
- 以使用简单、节省内存著称
- 导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!--xls07-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!--时间格式化-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.5</version>
</dependency>
* 03版的Excel最多可以放入65 536条数据,但是新版的是没有条数限制的
* 03版的后缀是xls,07版是xlsx
2.基本概念
3.创建步骤
- 工作蒲--工作表---行---列(单元格)
- 03版表格输出
// 1.创建工作蒲
HSSFWorkbook workbook = new HSSFWorkbook();
// 2.创建工作表
Sheet sheet = workbook.createSheet("统计表");
// 3.创建行
Row row1 = sheet.createRow(0);
// 4.创建单元格
Cell cell1 = row1.createCell(0);
cell1.setCellValue("今日新增人数");
Cell cell2 = row1.createCell(1);
cell2.setCellValue(666);
// 生成表(IO)
FileOutputStream outputStream = new FileOutputStream(PATH + "/统计表03.xls");
// 输出
workbook.write(outputStream);
System.out.println("Excel表格生成成功!");
// 关闭Excel
outputStream.close();
- 07版表格输出
// 1.创建工作蒲
XSSFWorkbook workbook = new XSSFWorkbook();
// 2.创建工作表
Sheet sheet = workbook.createSheet("统计表");
// 3.创建行
Row row1 = sheet.createRow(0);
// 4.创建单元格
Cell cell1 = row1.createCell(0);
cell1.setCellValue("今日新增人数");
Cell cell2 = row1.createCell(1);
cell2.setCellValue(666);
// 生成表(IO)
FileOutputStream outputStream = new FileOutputStream(PATH + "/统计表07.xlsx");
// 输出
workbook.write(outputStream);
System.out.println("Excel表格生成成功!");
// 关闭Excel
outputStream.close();
- 注意对象的区别和输出文件的格式后缀
- 03版大数据写HSSF
a. 实现
b. 速度快,但是只能写入65535条数据
long startTime = System.currentTimeMillis();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < 65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream outputStream = new FileOutputStream(PATH + "/大数据03.xls");
workbook.write(outputStream);
System.out.println("03版表格生成成功");
outputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("03版消耗时间:" + (endTime - startTime));
- 07版大数据写XSSF
- 速度慢,但是可以大量的写,理论上是无限的,实现
long startTime = System.currentTimeMillis();
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
for (int i = 0; i < 65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream outputStream = new FileOutputStream(PATH + "/大数据07.xlsx");
workbook.write(outputStream);
System.out.println("07版表格生成成功");
outputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("07版消耗时间:" + (endTime - startTime));
- 快速版SXSSF
- 可以写非常大量的数据,写数据速度快,占用内存更少
- 需要注意
- 会产生临时文件,需要清理临时文件
- 默认先写100条记录保存在内存中,超过数量最前面的数据被写入临时文件
- 使用new SXSSFWorkbook(数量)可以自定义
- 实现
long startTime = System.currentTimeMillis();
SXSSFWorkbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet();
for (int i = 0; i < 65536; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j);
}
}
FileOutputStream outputStream = new FileOutputStream(PATH + "/大数据07Super.xlsx");
workbook.write(outputStream);
// 清除临时文件
workbook.dispose();
System.out.println("07Super版表格生成成功");
outputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("07Super版消耗时间:" + (endTime - startTime));
四、EasyExcel操作
参考地址:https://www.yuque.com/easyexcel/doc/easyexcel
- 导入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> </dependency>
- 创建bean
@ExcelProperty("字符串标题") private String string; @ExcelProperty("日期标题") private Date date; @ExcelProperty("数字标题") private Double doubleData; /** * 忽略这个字段 */ @ExcelIgnore private String ignore;
- 写操作
private static final String PATH = "F:/springdemo";
private List<EasyExcelDemoData> data() {
List<EasyExcelDemoData> list = new ArrayList<EasyExcelDemoData>();
for (int i = 0; i < 10; i++) {
EasyExcelDemoData data = new EasyExcelDemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
@Test
public void easyExcelWrite() {
String fileName = PATH + "/easyExcel测试.xlsx";
EasyExcel.write(fileName, EasyExcelDemoData.class).sheet("模板").doWrite(data());
}