POI和easyExcel
概述
常用场景
1.将用户信息导出为excel表格(导出数据)
2.将excel表中的信息录入到网站数据库(习题上传),大大减轻网站录入量!
开发中经常会涉及到excel的处理,如导出excel,导入excel到数据库中。
操作excel目前比较流行的就是Apache POI和阿里巴巴的 easyExcel.
Apache POI
会比较麻烦


easyExcel

EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单、节省内存著称。
EasyExcel能大大减少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。
下图是EasyExcel和POI在解析Excel的对比图

官方文档:https://www.yuque.com/easyexcel/doc/about
POI-Excel写
基本使用
导入依赖
<!--导入依赖--><dependencies><!--java万物皆对象--><!--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>
03版本
@Testpublic void testWriter03() throws Exception {//1.创建一个工作簿 03Workbook workbook = new HSSFWorkbook();//2.创建一个工作表Sheet sheet = workbook.createSheet("观众统计表");//3.创建一个行 (1,1)Row row1 = sheet.createRow(0);//4.创建一个单元格Cell cell11 = row1.createCell(0);cell11.setCellValue("今日新增观众");//(1,2)Cell cell12 = row1.createCell(1);cell12.setCellValue(666);//第二行(2,1)Row row2 = sheet.createRow(1);Cell cell21 = row2.createCell(0);cell21.setCellValue("统计时间");//(2,2)Cell cell22 = row2.createCell(1);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell22.setCellValue(time);//生成一张表(IO流) 03版本就是使用xls结尾FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表03.xls");workbook.write(fileOutputStream);//关闭流fileOutputStream.close();System.out.println("观众统计表03.xls生成完毕");}
07版本
@Testpublic void testWriter07() throws Exception {//1.创建一个工作簿 07Workbook workbook = new XSSFWorkbook();//2.创建一个工作表Sheet sheet = workbook.createSheet("观众统计表");//3.创建一个行 (1,1)Row row1 = sheet.createRow(0);//4.创建一个单元格Cell cell11 = row1.createCell(0);cell11.setCellValue("今日新增观众");//(1,2)Cell cell12 = row1.createCell(1);cell12.setCellValue(666);//第二行(2,1)Row row2 = sheet.createRow(1);Cell cell21 = row2.createCell(0);cell21.setCellValue("统计时间");//(2,2)Cell cell22 = row2.createCell(1);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell22.setCellValue(time);//生成一张表(IO流) 03版本就是使用xls结尾FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表07.xlsx");workbook.write(fileOutputStream);//关闭流fileOutputStream.close();System.out.println("观众统计表07.xlsX生成完毕");}
注意一个对象的区别,文件后缀!
数据批量导入
大文件写HSSF(03)
缺点:最多只能处理65536行,否则抛出异常
优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快
@Testpublic void testWriterBigData03() throws Exception {//时间long begin = System.currentTimeMillis();//创建一个簿Workbook workbook = new HSSFWorkbook();//创建表Sheet sheet = workbook.createSheet();//写入数据for (int rowNumber = 0; rowNumber < 65536; rowNumber++) {Row row = sheet.createRow(rowNumber);for (int cellNum = 0; cellNum < 10; cellNum++) {Cell cell = row.createCell(cellNum);cell.setCellValue(cellNum);}}System.out.println("over");FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWriterBigData03.xls");workbook.write(fileOutputStream);fileOutputStream.close();long end = System.currentTimeMillis();System.out.println((double) (end - begin) / 1000);}
大文件写XSSF(07)
缺点:写数据时操作非常慢,非常耗内存,也会发生内存溢出,如100万条
优点:可以写较大的数据量,如20万条
@Testpublic void testWriterBigData07() throws Exception {//时间long begin = System.currentTimeMillis();//创建一个簿Workbook workbook = new XSSFWorkbook();//创建表Sheet sheet = workbook.createSheet();//写入数据for (int rowNumber = 0; rowNumber < 65537; rowNumber++) {Row row = sheet.createRow(rowNumber);for (int cellNum = 0; cellNum < 10; cellNum++) {Cell cell = row.createCell(cellNum);cell.setCellValue(cellNum);}}System.out.println("over");FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWriterBigData07.xlsx");workbook.write(fileOutputStream);fileOutputStream.close();long end = System.currentTimeMillis();System.out.println((double) (end - begin) / 1000);}
大文件写SXSSF
优点:可以写非常大的数据量,如100万条甚至更多,写数据速度快,占用更少的内存
@Testpublic void testWriterBigData07S() throws Exception {//时间long begin = System.currentTimeMillis();//创建一个簿Workbook workbook = new SXSSFWorkbook();//创建表Sheet sheet = workbook.createSheet();//写入数据for (int rowNumber = 0; rowNumber < 65537; rowNumber++) {Row row = sheet.createRow(rowNumber);for (int cellNum = 0; cellNum < 10; cellNum++) {Cell cell = row.createCell(cellNum);cell.setCellValue(cellNum);}}System.out.println("over");FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWriterBigData07S.xlsx");workbook.write(fileOutputStream);fileOutputStream.close();//清除临时文件!((SXSSFWorkbook)workbook).dispose();long end = System.currentTimeMillis();System.out.println((double) (end - begin) / 1000);}
注意:
过程中会产生临时文件,需要清理临时文件默认由100条记录被保存在内存中,如果超过这数量,则最前面的数据被写入临时文件如果想自定义内存中数据的数量,可以使用 new SXSSFWorkBook(数量)
SXSSFWorkbook来自官方的解释:实现“BigGridDemo”策略的流式XSSFWorkbook版本。这允许写入非常大的文件而不会耗尽内存,因为任何时候只有可配置的行部分被保存在内存中。
请注意,仍然可能会消耗大量内存,这些内存基于您正在使用的功能,例如合并区域,注释……仍然只存储在内存中,因此如果广泛使用,可能需要大量内存。
POI-Excel读
03 | 07
03版本
@Testpublic void testRead03() throws Exception {//获取文件流FileInputStream fileInputStream = new FileInputStream(PATH + "观众统计表03.xls");//1.创建一个工作簿。 使用excel能操作的这边他都可以操作Workbook workbook = new HSSFWorkbook(fileInputStream);//2.得到表Sheet sheet = workbook.getSheetAt(0);//3.得到行Row row = sheet.getRow(0);//4.得到列Cell cell = row.getCell(1);//读取值的时候,一定要注意类型//getStringCellValue 字符串类型// System.out.println(cell.getStringCellValue());System.out.println(cell.getNumericCellValue());fileInputStream.close();}
07版本
@Testpublic void testRead07() throws Exception {//获取文件流FileInputStream fileInputStream = new FileInputStream(PATH + "观众统计表07.xlsx");//1.创建一个工作簿。 使用excel能操作的这边他都可以操作Workbook workbook = new XSSFWorkbook(fileInputStream);//2.得到表Sheet sheet = workbook.getSheetAt(0);//3.得到行Row row = sheet.getRow(0);//4.得到列Cell cell = row.getCell(1);//读取值的时候,一定要注意类型//getStringCellValue 字符串类型// System.out.println(cell.getStringCellValue());System.out.println(cell.getNumericCellValue());fileInputStream.close();}
注意获取的类型即可
读取不同的数据类型(麻烦)
@Testpublic void testCellType() throws Exception {//获取文件流FileInputStream fileInputStream = new FileInputStream(PATH + "明细表.xls");//1.创建一个工作簿。 使用excel能操作的这边他都可以操作Workbook workbook = new HSSFWorkbook(fileInputStream);//获取标题内容Sheet sheet = workbook.getSheetAt(0);Row rowTitle = sheet.getRow(0);if (rowTitle != null) {//一定要掌握int cellCount = rowTitle.getPhysicalNumberOfCells();for (int cellNum = 0; cellNum < cellCount; cellNum++) {Cell cell = rowTitle.getCell(cellNum);if (cell != null) {String cellValue = cell.getStringCellValue();System.out.print(cellValue + "|");}}System.out.println();}//获取表中的内容int rowCount = sheet.getPhysicalNumberOfRows();for (int rowNum = 1; rowNum < rowCount; rowNum++) {Row rowdata = sheet.getRow(rowNum);if (rowdata != null) {//读取列int cellCount = rowTitle.getPhysicalNumberOfCells();for (int cellNum = 0; cellNum < cellCount; cellNum++) {System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]");Cell cell = rowdata.getCell(cellNum);//匹配列的数据类型if (cell != null) {CellType cellType = cell.getCellType();String cellValue = "";switch (cellType) {case STRING:System.out.println("String类型");cellValue = cell.getStringCellValue();break;case _NONE:System.out.println("_NONE类型");break;case NUMERIC://数组(日期、普通数字)System.out.println("NUMERIC类型");if (DateUtil.isCellDateFormatted(cell)) {//日期System.out.println("日期");Date date = cell.getDateCellValue();cellValue = new DateTime(date).toString("yyyy-MM-dd");} else {// 是数字的转换成字符串获取,防止数字过长System.out.println("转换为字符串");HSSFDataFormatter hssfDataFormat = new HSSFDataFormatter();cellValue = hssfDataFormat.formatCellValue(cell);}break;case BLANK:System.out.println("BLANK类型");break;case ERROR:System.out.println("数据类型错误");break;}System.out.println(cellValue);}}}}fileInputStream.close();}}
注意类型转换问题
计算公式
了解即可
@Testpublic void testFormula() throws Exception {//获取文件流FileInputStream fileInputStream = new FileInputStream(PATH + "公式.xls");//1.创建一个工作簿。 使用excel能操作的这边他都可以操作Workbook workbook = new HSSFWorkbook(fileInputStream);Sheet sheet = workbook.getSheetAt(0);Row row = sheet.getRow(4);Cell cell = row.getCell(0);//拿到计算公式 evalFormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);//输出单元格的内容CellType cellType = cell.getCellType();switch (cellType) {case FORMULA:String formula = cell.getCellFormula();System.out.println(formula);//计算CellValue evalute = formulaEvaluator.evaluate(cell);String cellValue = evalute.formatAsString();System.out.println(cellValue);break;}fileInputStream.close();}
EasyExcel操作
导入依赖
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.0.0-beta3</version></dependency>
写入测试
https://www.yuque.com/easyexcel/doc/write
读取测试
https://www.yuque.com/easyexcel/doc/read
固定套路:
1、写入,固定类格式进行写入
2、读取,根据监听器设置的规则进行读取!
