POI
easyExcel
easyExcel 官网地址
EasyExcel 是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。
EasyExcel 能大大减少占用内存的主要原因是在解析 Excel 时没有将文件数据一次性全部加载到内存中,
而是从磁盘上一行行读取数据,逐个解析。
下图是 EasyExcel 和 POI 在解析Excel时的对比图。
POI快速开始
写
- 创建项目
- 导入依赖
由于excel分为03和07版的所以下面两个版本都导入了,以便都练习一下,推荐使用07版本
<dependencies>
<!--xls(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<!--test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
03 | 07 版本的写,就是对象不同,方法一样的!
需要注意:2003 版本和 2007 版本存在兼容性的问题!03最多只有 65535 行!
03版本:
@Test
public void testWrite03() throws Exception {
System.out.println("开始");
// 创建工作簿
Workbook workbook = new HSSFWorkbook();
// 创建工作表
Sheet sheet = workbook.createSheet("人员表");
// 创建行
Row row = sheet.createRow(0);
// 创建单元格
Cell cell01 = row.createCell(0);
cell01.setCellValue("张三");
// 创建单元格
Cell cell02 = row.createCell(1);
cell02.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
//创建输出流,将新建的文件输出
FileOutputStream outputStream = new FileOutputStream(PATH+"统计表03.xls");
// 输出
workbook.write(outputStream);
// 关闭流
outputStream.close();
System.out.println("结束");
}
07版本
@Test
public void testWrite07() throws Exception{
System.out.println("开始");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("人员表");
Row row = sheet.createRow(0);
Cell cell01 = row.createCell(0);
cell01.setCellValue("skr");
Cell cell02 = row.createCell(1);
cell02.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
FileOutputStream outputStream = new FileOutputStream(PATH+"统计表07.xlsx");
workbook.write(outputStream);
outputStream.close();