1.应用场景
- 将用户信息导出为excel表格
- 讲Excel表中的信息录入到网站数据库,大大减小网站数据的录入量!
开发中经常会涉及到excel的处理,如导出Excel到数据库中!
操作Excel目前比较流行的就是Apache POI和阿里巴巴的easyExcel
2.Apache POI
简介
Apache POI官网: https://poi.apache.org/
HSSF 对应 Excel 03 版本,最多支持65535行
XSSF对应 Excel 07 版本,行数无限制
缺点:
- 使用比较麻烦
- 数据量大的时候会可能报OOM异常
项目准备
创建maven项目,
<dependencies>
<!--xLs(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!--xLsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</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>
创建两个版本的Excel文件
打开可以看到,03版最多支持到65536行,而07版不受限制,理论上无限
二者文件名后缀不同,对应操作的Java工具类也不同
明确几个概念,工作簿、工作表、行、单元格,分别对应了各自的对象
全部代码
@Test
public void testwirte() throws IOException {
String path = "D:\\IDEAWorkspace\\EsaypoiAndExecl\\";
//1 创建一个工作蒲
Workbook workbook = new HSSFWorkbook();
//2 创建一个工作表 (因为工作蒲包含工作表)
Sheet sheet = workbook.createSheet("小风粉丝");
//3 创建一个行,表里面有行,0代表起始位置为第一行
Row row = sheet.createRow(0);
//4 创建一个单元格
Cell cell00 = row.createCell(0);
cell00.setCellValue("今日新增粉丝");
Cell cell01 = row.createCell(1);
cell01.setCellValue(0);
//创建第二个行
Row row1 = sheet.createRow(1);
Cell cell02 = row1.createCell(0);
cell02.setCellValue("统计时间");
Cell cell11 = row1.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell11.setCellValue(time);
//生成一张表 (io流) 03版本就用xls结尾
FileOutputStream fileOutputStream = new FileOutputStream(path + "小风新增分析表03.xls");
//输出
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
System.out.println("表格生成完毕");
}
@Test
public void testwirte07() throws Exception {
String path = "D:\\IDEAWorkspace\\EsaypoiAndExecl\\";
//1 创建一个工作蒲
Workbook workbook = new XSSFWorkbook();
//2 创建一个工作表 (因为工作蒲包含工作表)
Sheet sheet = workbook.createSheet("小风粉丝");
//3 创建一个行,表里面有行,0代表起始位置为第一行
Row row = sheet.createRow(0);
//4 创建一个单元格
Cell cell00 = row.createCell(0);
cell00.setCellValue("今日新增粉丝");
Cell cell01 = row.createCell(1);
cell01.setCellValue(0);
//创建第二个行
Row row1 = sheet.createRow(1);
Cell cell02 = row1.createCell(0);
cell02.setCellValue("统计时间");
Cell cell11 = row1.createCell(1);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell11.setCellValue(time);
//生成一张表 (io流) 03版本就用xlsx结尾
FileOutputStream fileOutputStream = new FileOutputStream(path + "小风新增分析表07.xlsx");
//输出
workbook.write(fileOutputStream);
//关闭流
fileOutputStream.close();
System.out.println("表格生成完毕");
}
写入大量数据时 hssf 较快
@Test public void testwrite03BigData() throws IOException { String path = "D:\\IDEAWorkspace\\EsaypoiAndExecl\\"; //时间 long begin = System.currentTimeMillis(); //创建一个薄 Workbook workbook = new HSSFWorkbook(); //创建表 Sheet sheet = workbook.createSheet(); //写入数据 for (int rowNum = 0; rowNum < 65536; rowNum++) { Row row = sheet.createRow(rowNum); for (int cellNum = 0; cellNum < 10; cellNum++) { Cell cell = row.createCell(cellNum); cell.setCellValue(cellNum); } } FileOutputStream fileOutputStream = new FileOutputStream(path + "03版本Excel大量数据测试.xls"); workbook.write(fileOutputStream); fileOutputStream.close(); System.out.println("over"); long end = System.currentTimeMillis(); System.out.println((double) (end - begin) / 1000); }
over
1.346
使用xssf时,处理数据较慢
@Test public void testwrite03BigDatax() throws IOException { String path = "D:\\IDEAWorkspace\\EsaypoiAndExecl\\"; //时间 long begin = System.currentTimeMillis(); //创建一个薄 Workbook workbook = new XSSFWorkbook(); //创建表 Sheet sheet = workbook.createSheet(); //写入数据 for (int rowNum = 0; rowNum < 65537; rowNum++) { Row row = sheet.createRow(rowNum); for (int cellNum = 0; cellNum < 10; cellNum++) { Cell cell = row.createCell(cellNum); cell.setCellValue(cellNum); } } FileOutputStream fileOutputStream = new FileOutputStream(path + "07版本Excel大量数据测试.xlsx"); workbook.write(fileOutputStream); fileOutputStream.close(); System.out.println("over"); long end = System.currentTimeMillis(); System.out.println((double) (end - begin) / 1000); }
over
6.891
使用super加速版sxssf
大文件写SXSSF-07升级版
优点:可以写非常大量的数据库,如100万条甚至更多条,写数据速度快,占用更少的内存
注意:
过程中会产生临时文件,需要在程序运行结束后清理临时文件
默认由100条记录被保存在内存中,如果超出这数量,则最前面的数据被写入临时文件
如果想自定义内存中数据的数量,可以使用new SXSSFWorkbook(数量)
@Test
public void testwrite03BigDataxs() throws IOException {
String path = "D:\\IDEAWorkspace\\EsaypoiAndExecl\\";
//时间
long begin = System.currentTimeMillis();
//创建一个薄
Workbook workbook = new SXSSFWorkbook();
//创建表
Sheet sheet = workbook.createSheet();
//写入数据
for (int rowNum = 0; rowNum < 100000; rowNum++) {
Row row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 10; cellNum++) {
Cell cell = row.createCell(cellNum);
cell.setCellValue(cellNum);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(path + "07版本Excel大量数据测试super.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("over");
long end = System.currentTimeMillis();
//清除临时缓存
((SXSSFWorkbook)workbook).dispose();
System.out.println((double) (end - begin) / 1000);
}
写入十行条数据仅需4.026
注意:
//清除临时缓存
((SXSSFWorkbook)workbook).dispose();
SXSSWorkbook 来自官方解释:实现:BigGridDemo策略的流式XSSFWorkbook版本。这允许写入非常大的文件而不会耗尽内存,因为任何时候只有可配置的行部分被保存在内存中。
POI-Excel读
03类型
public class Poiread { String path = "D:\\IDEAWorkspace\\EsaypoiAndExecl\\"; @Test public void testRead() throws IOException { FileInputStream fileInputStream = new FileInputStream(path+"小风新增分析表03.xls"); //创建一个工作蒲 Workbook workbook = new HSSFWorkbook(fileInputStream); //得到表 根据索引 Sheet sheet = workbook.getSheetAt(0); //得到行 Row row = sheet.getRow(0); //得到cell Cell cell = row.getCell(0); System.out.println(cell.getStringCellValue()); fileInputStream.close(); } }
狂神视频到此结束,后面的方法多数过时,切没有讲到注解方式
狂神版 不推荐