EasyExcel与POI

执行流程与区别

tip:07版本的Excel最多有104万

先引入pom

 <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version></dependency><!-- https://mvnrepository.com/artifact/joda-time/joda-time --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.11.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.2</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version></dependency>

POI的写入

public void testWriter03() throws Exception {//创建接收路径String path="D:\\EasyExcel\\";//创建工作簿Workbook workbook=new XSSFWorkbook();//创建工作表Sheet sheet=workbook.createSheet("核算统计表");
//        创建第一行Row row=sheet.createRow(0);Cell cell=row.createCell(0);cell.setCellValue("今日新增");Cell cell1=row.createCell(1);cell1.setCellValue(666);
//        创建第二行Row row1=sheet.createRow(1);Cell cell2=row1.createCell(0);cell2.setCellValue("人数");Cell cell3=row1.createCell(1);cell3.setCellValue("sdw");
//        生成一张表FileOutputStream fileOutputStream=new FileOutputStream(path+"hello.xlsx");workbook.write(fileOutputStream);fileOutputStream.close();}
测试生成百万数据的时间
  public void Test() throws Exception {//创建一个存放地址Date date=new Date();String  path="D:\\EasyExcel\\";//创建一个工作簿Workbook workbook=new SXSSFWorkbook();//创建工作表Sheet sheet=workbook.createSheet("填满表");Row row ;Cell cell = null;for(int i=0;i<1000000 ;i++){row= sheet.createRow(i);for (int j=0;j<10;j++){cell=row.createCell(j);cell.setCellValue(j);}}FileOutputStream fileOutputStream=new FileOutputStream(path+"Test.xlsx");workbook.write(fileOutputStream);fileOutputStream.close();Date date1=new Date();long from=date.getTime();long end=date1.getTime();System.out.println(end-from);}

电脑跑完11秒多

POI的读取

 public void Test() throws Exception {//路径地址String  path="D:\\EasyExcel\\";FileInputStream inputStream=new FileInputStream(path+"hello.xlsx");//创建工作簿Workbook workbook=new XSSFWorkbook(inputStream);Sheet sheet= workbook.getSheetAt(0);Row row=sheet.getRow(0);Cell cell=row.getCell(0);System.out.println(cell.getStringCellValue());inputStream.close();}

读取多个,需要对每行数据的类型进行识别

  public void Test() throws Exception {String path = "D:\\EasyExcel\\";FileInputStream inputStream = new FileInputStream(path + "hello.xlsx");//创建一个工作簿Workbook workbook = new XSSFWorkbook(inputStream);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) {CellType cellType = cell.getCellType();String cellValue = cell.getStringCellValue();System.out.print(cellValue + " | ");}}System.out.println();}int rowCount = sheet.getPhysicalNumberOfRows();for (int rowNumber = 1; rowNumber < rowCount; rowNumber++) {Row rowData = sheet.getRow(rowNumber);if (rowData!=null){int cellCount = rowTitle.getPhysicalNumberOfCells();for (int cellNumber=0;cellNumber<cellCount;cellNumber++){System.out.print("["+(rowNumber+1)+"-"+(cellNumber+1)+"]");Cell cell=rowData.getCell(cellNumber);if (cell!=null){CellType cellType = cell.getCellType();String cellValue = "";switch (cellType){case STRING:System.out.print("字符串类型");cellValue=cell.getStringCellValue();break;case BOOLEAN:System.out.print("布尔类型");cellValue=String.valueOf(cell.getBooleanCellValue());break;case BLANK:System.out.print("空类型");break;case NUMERIC:System.out.print("数字类型");if(DateUtil.isCellDateFormatted(cell)){System.out.print("日期");Date date= cell.getDateCellValue();cellValue = new DateTime(date).toString("yyyy-MM-dd");}else {System.out.print("普通数字");cellValue= cell.toString();}break;case ERROR:System.out.print("类型错误");break;}System.out.println(cellValue);}}}}}

计算公式的使用实例

public void Test2() throws Exception {String path = "D:\\EasyExcel\\";FileInputStream inputStream=new FileInputStream(path+"hello.xlsx");Workbook workbook=new XSSFWorkbook(inputStream);Sheet sheet=workbook.getSheetAt(0);Row row=sheet.getRow(4);Cell cell=row.getCell(0);//拿到计算公式FormulaEvaluator formulaEvaluator =new HSSFFormulaEvaluator((HSSFWorkbook) workbook);//输出单元格的内容CellType cellType= cell.getCellType();switch (cellType){case FORMULA:String formula =cell.getCellFormula();System.out.println(formula);//计算CellValue cellValue=formulaEvaluator.evaluate(cell);String cel=cellValue.formatAsString();System.out.println(cel);break;}}

EasyExcel是一个基于Java简单的,省内存的读写Excel的开源项目,具体的可以官网上查看,找不到源码的话可以私信我

先引入pom依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.1.1</version>
</dependency>
写入
@Slf4j
public class Data {private List<DemoData> data() {List<DemoData> list = ListUtils.newArrayList();for (int i = 0; i < 10; i++) {DemoData data = new DemoData();data.setString("字符串" + i);data.setDate(new Date());data.setDoubleData(0.56);list.add(data);}return list;}/*** 最简单的写* <p>* 1. 创建excel对应的实体对象 参照{@link DemoData}* <p>* 2. 直接写即可*/@Testpublic void simpleWrite() {// 注意 simpleWrite在数据量不大的情况下可以使用(5000以内,具体也要看实际情况),数据量大参照 重复多次写入// 写法1 JDK8+// since: 3.0.0-beta1String fileName = "D:\\EasyExcel\\EasyTest.xlsx";// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭// 如果这里想使用03 则 传入excelType参数即可EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(() -> {// 分页查询数据return data();});
模板
public class DemoData {@ExcelProperty("字符串标题")private String string;@ExcelProperty("日期标题")private Date date;@ExcelProperty("数字标题")private Double doubleData;/*** 忽略这个字段*/@ExcelIgnoreprivate String ignore;
}
读取
   @Testpublic void simpleRead() {// 写法1:JDK8+ ,不用额外写一个DemoDataListener// since: 3.0.0-beta1String fileName = "D:\\EasyExcel\\EasyTest.xlsx";// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭// 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {for (DemoData demoData : dataList) {log.info("读取到一条数据{}", JSON.toJSONString(demoData));}})).sheet().doRead();
固定套路

1.写入,固定类格式进行写入

2.读取,根据监听器 设置的规则进行读取

EasyExcel与POI相关推荐

  1. easyexcel和poi对比_POI 和 EasyExcel

    POI 和 easyExcel 讲解 转自狂神老师,仅作为个人笔记使用 一.POI 常用进程 1.将用户信息导出为excel表格(导出数据....) 2.将Excel表中的信息录入到网站数据库(习题上 ...

  2. EasyExcel和POI学习笔记

    EasyExcel和POI使用详解 一.Poi-Excel 写 1.常规入门 POI是Apache软件基金会的,POI为"Poor Obfuscation Implementation&qu ...

  3. easyexcel和poi是否有版本冲突_easyexcel--解决poi大文件发生OOM问题

    问题复现 工作中,项目里的导入功能采用了poi读取然后进行业务操作,在导入50M文件时发生了OOM报错信息,以下是本地复现的错误信息(由于环境不一样,本地导入14M的文件就已出现错误) 究其原因 项目 ...

  4. 解决easyExcel和poi版本冲突问题

    由于easyExcel自带依赖于3.17的poi,所以如果项目其它地方引入了其它版本的poi,就会导致easyExcel导出失败等一系列问题,主要原因是因为poi版本冲突导致的,这里我提供的解决办法是 ...

  5. springboot导出excel(easyexcel和poi 列下拉及表格锁定)

    最近做的项目导入的数据量比较大,直接用poi或者easypoi会可能会出现OOM的情况,综合考虑下用easyexcel, pom引入所需包 <!-- https://mvnrepository. ...

  6. EasyExcel的简单使用(easyExcel和poi)

    EasyExcel的简单使用 前言 Excel读 1.实体类 2.读监听器与测试类 3.输出结果 Excel写 1.实体类 2.写入Excel的测试类 3.输出结果 填充Excel 1.Excel模板 ...

  7. easyexcel和poi是否有版本冲突_记使用EasyPoi导出Excel遇到的问题

    报错: [2019-01-0414:31:07]172.20.2.150 content:[14:31:07.977][52][956DE36473C8E283]ERRORc.a.e.e.e.t.Ex ...

  8. 手把手教你用java读写excel表格文件(POI,EasyExcel)

    视频链接-我是学习之星我为狂神打call~ [狂神说Java]POI及EasyExcel一小时搞定通俗易懂 想给项目添加一个表格导入导出功能吗? "xxx管理系统"没有导入导出功能 ...

  9. 使用POI和EasyExcel实现Excel导入和导出功能

    需求场景 开发中经常会设计到excel的处理,需求场景如下所示: 1.将用户信息导出为excel表格(导出数据) 2.将Excel表中的信息录入到数据库中(导入数据) 操作Excel目前比较流行的就是 ...

最新文章

  1. 在安装sql server时出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机”错误。...
  2. SQLServer Agent执行[分发清除: distribution] 无法删除快照文件
  3. Extjs之RowNumberer
  4. Leaflet笔记-把leaflet-tilelayer-wmts移植到vue cli中(含思路)
  5. mysql dump还原_mysql dump备份和恢复
  6. 在Windows2003 SP2上安装VS2005SP1遇到的问题
  7. chrome介绍与用法
  8. Tensorflow中与张量形状有关的操作
  9. 如何写程序自动下载BBC Learning English的所有在线课程
  10. 五笔打字--思成五笔秘方
  11. cubic算法优化_CUBIC拥塞拥塞控制算法
  12. 爆款预订,2022 最值得关注的后台框架——Fantastic-admin
  13. 简约至上:你必须知道的产品设计
  14. vue数据传递--父传子-方法传递
  15. php中composer require和composer require --dev的区别
  16. MATLAB 2016a系统错误解决方案
  17. cin、cout、cerr、clog用法区别及其在VS环境下的重定向
  18. Android EditText 监听回车键
  19. CTU Open Contest 2017 Pond Cascade
  20. 2022-2027年中国煤制尿素行业市场全景评估及发展战略规划报告

热门文章

  1. 认认真真推荐几个Python、统计学、算法、机器学习...等方向的公众号
  2. IDEA--如何解决Java:程序包xxxx不存在
  3. Java数据结构——直接插入排序+希尔排序+冒泡排序
  4. 使用adb命令清除预装内置的第三方app
  5. stricmp linux 头文件,头文件stdio.hstdlib.hstring.h
  6. html控件透明与背景透明
  7. 基于FPGA的波、幅、频、相可调DDS信号发生器的设计
  8. 保监会:保险机构应坚持长期投资
  9. 保险业首季度保费收入猛增五成
  10. adc0809c语言编程,单片机C语言程序设计之ADC0809数模转换与显示