通过Apache POI对Excel文件进行解析和通过Alibaba EasyExcel对大数据量的Excel进行解析
通过Apache POI对excel文件进行解析和通过Alibaba EasyExcel对大数据量的excel进行解析
我们知道excel文件是一种数据表格文件,在开发过程中我们经常要对Excel文件进行数据的导入与导出。
常见的对Excel文件处理的技术流主要包括:Apache POI、JXL和Alibaba EasyExcel。
Apache POI是将文件直接加载到内存中,其速度非常快,所以适用于数据量不大的文件。
Alibaba EasyExcel是采取逐行读取的方式,比较适用于数据量较大的文件。
通过Apache POI对Excel文件进行解析
Apache POI它提供了对不同格式文件的解析:
HSSF-> 提供读写Excel格式文件的功能
XSSF-> 提供读写Excel格式文件的功能
HWPF -> 提供读写Word格式文件的功能
HSLF-> 提供读写PowerPoint格式文件的功能
HDGF -> 提供读写Visio格式文件的功能
接下来我们将会使用XSSFWorkbook实现类对Excel文件进行进一步解析。
1.Workbook(Excel文件)
1.1 创建一个Excel文件
// 使用Try-with捕获异常
try (Workbook workbook = new XSSFWorkbook();
// 使用FileOutputStream把建立excel文件的目录路径和命名传入
FileOutputStream fos = new FileOutputStream("D:\\test\\text.xlsx")) {
// 将Workbook对象中包含的数据,通过输出流,写入至Excel文件
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
}
1.2 加载一个Excel文件
// 通过输入流,读取excel文件
FileInputStream in = new FileInputStream("D:\\text\\text.xlsx");
// excel文件对象
Workbook workbook = new XSSFWorkbook(in);
2.Sheet(工作簿)
通过Workbook创建Sheet
2.1 创建工作簿
// 创建工作簿Sheet
// 按照默认方法创建工作簿
Sheet sheet0 = workbook.createSheet();
// 按照自定义创建工作簿
Sheet sheet1 = workbook.createSheet("喜羊羊");
Sheet sheet2 = workbook.createSheet("美羊羊");
2.2 获取工作簿
// 获取工作簿
// 通过工作簿的名字获取工作簿
Sheet sheet = workbook.getSheet("sheet0");
// 通过工作簿的下标获取工作簿
Sheet sheet = workbook.getSheetAt(0);
3.Row(数据行)
3.1 创建行
// 创建数据行Row
Row row = sheet0.createRow(0);
3.2 获取行
// 根据指定下标获取指定行
Row row = sheet0.getRow(0);
3.3 获取首行下标
// 获取首行下标
Row row = sheet.getFirstRowNum();
3.4 获取尾行下标
// 获取尾行下标
Row row = sheet.getLastRowNum();
3.5 遍历行
3.5.1 遍历工作簿中的所有行
for(Row row : sheet) {
System.out.println(row);
}
因为Row中包含迭代器,所以我们可以通过for each对其进行全部遍历。
3.5.2 遍历工作簿中的部分行
// 通过行下标,遍历工作簿中的部分行
for(int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
System.out.println(row);
}
4.Cell(单元格)
4.1 创建单元格
Cell cell = row.createCell(0);
4.2 获取单元格
// 根据下标获取指定单元格
Cell cell = row.getCell(1);
4.3 遍历所有单元格
for(Cell cell : row) {
System.out.println(cell);
}
4.4 获取单元格样式
CellType type = cell.getCellType();
4.5 设置单元格格式(以时间格式为例:****年**月**日 **(小时):**(分钟):**(秒))
// 获取格式编码值
DataFormat dataformat = workbook.createDataFormat();
short dateFormatCode = dataformat.getFormat("yyyy年MM月dd日 HH:ss:mm");
// 创建日期格式对象
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(dateFormatCode); // 设置格式编码值
最后在给单元格赋值的时候将创建好的格式导入单元格格式
Cell cell = row.createCell(0); // 日期
cellc.setCellStyle(dateCellStyle); // 日期格式对象
cellc.setCellValue(new Date());
通过Alibaba EasyExcel对大数据量的Excel进行解析
EasyExcel与POI对Excel文件的读取方式不同,所以导致两者一般作用于数据量不同的Excel文件。
package com.zwd;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import com.zwd.entity.Order;
public class Demo01 {
public static void main(String[] args) {
long begin = System.currentTimeMillis();
// 写入100w
EasyExcel.write("D:\\poi\\easy100w.xlsx", Order.class)
.sheet("订单列表")
.doWrite(data());
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - begin) + "毫秒");
}
// 创建100w条订单数据
private static List<Order> data() {
List<Order> list = new ArrayList<Order>();
for (int i = 0; i < 1000000; i++) {
list.add(new Order());
}
return list;
}
}
可以看到当使用EasyExcel写入100w条数据时耗时15597毫秒。
而当使用POI对大数据量的Excel文件读写时,通常采用SXSSFWorkbook实现类,而不是XSSFWorkbook实现类。
package com.zwd;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Demo09 {
public static void main(String[] args) {
// POI:使用SXSSFWorkbook对象
try (Workbook workbook = new SXSSFWorkbook(1000);
FileOutputStream out = new FileOutputStream("D:\\poi\\100w.xlsx")) {
// 在"已存在的Excel文件"中,创建新的sheet
Sheet sheet = workbook.createSheet();
// 创建列头
Row headrow = sheet.createRow(0);
Cell cell0 = headrow.createCell(0);
cell0.setCellValue("序号");
Cell cell1 = headrow.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = headrow.createCell(2);
cell2.setCellValue("日期");
Cell cell3 = headrow.createCell(3);
cell3.setCellValue("红包金额");
// 获取格式编码值
DataFormat dataformat = workbook.createDataFormat();
short dateFormatCode = dataformat.getFormat("yyyy年MM月dd日 HH:ss:mm");
short moneyFormatCode = dataformat.getFormat("¥#,###");
// 创建日期格式对象
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setDataFormat(dateFormatCode); // 设置格式编码值 164
// 创建货币格式对象
CellStyle moneyCellStyle = workbook.createCellStyle();
moneyCellStyle.setDataFormat(moneyFormatCode); // 设置格式编码值
long begin = System.currentTimeMillis();
for(int i = 0; i < 1000000; i++) {
// 获取姓名
String name = "A" + i;
// 创建行
Row row = sheet.createRow(i + 1);
// 创建单元格
Cell cella = row.createCell(0); // 序号
cella.setCellValue(String.valueOf(i + 1));
Cell cellb = row.createCell(1); // 姓名
cellb.setCellValue(name);
Cell cellc = row.createCell(2); // 日期
cellc.setCellStyle(dateCellStyle); // 日期格式对象
cellc.setCellValue(new Date());
Cell celld = row.createCell(3); // 红包金额
celld.setCellStyle(moneyCellStyle); // 货币金额格式对象
celld.setCellValue((int)(Math.random() * 100000));
}
// 写入文件
workbook.write(out);
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - begin) + "毫秒");
} catch (IOException e) {
e.printStackTrace();
}
}
}
采用SXSSFWorkbook实现类时,可以选择生成一定量行数时直接转入到excel表中。
此代码中采用了1000行时,转入到excel表中。
不同的参数对运行时间也有影响。
当使用POI的SXSSFWorkbook实现类写入100w条数据时耗时10045毫秒。
而使用POI的XSSFWorkbook实现类时,如果当电脑的内存、CPU不足以支撑时,就会产生内存溢出。
在本人电脑上采用三种不同方式对相同数据量的Excel文件进行写入数据操作产生的结果:
电脑型号:联想拯救者Y7000 2019 1050 | 设备名称:LAPTOP-V387FFUD | 处理器:Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz 2.40 GHz | 机带RAM:8.00 GB |
技术流 | 数据量 | 耗时 | |
Alibaba EasyExcel | 100w | 15597毫秒 | |
POI SXSSFWorkbook实现类 | 100w | 10045毫秒 | |
POI XSSFWorkbook实现类 | 100w | 内存溢出 |
更多推荐
所有评论(0)