2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > SpringBoot使用poi或EasyExcel导入导出Excel文件

SpringBoot使用poi或EasyExcel导入导出Excel文件

时间:2021-07-16 02:54:59

相关推荐

SpringBoot使用poi或EasyExcel导入导出Excel文件

使用poi导入导出Excel

首先引入poi依赖包

03版本的Excel和07版本的Excel所需要的依赖不同,都需要导入。

记录问题

最初导入的poi包为3.6版本。WorkBook类和Sheet类不能通过下面这种方法直接创建

//1.创建工作簿Workbook workbook = new XSSFWorkbook();//2.创建一个工作表--sheetSheet sheet =workbook.createSheet("自测sheet");

需要通过下面这种方法细分,而这种方法创建的文件生成xlsx(07版)的文件时会导致文件损坏,将版本升值3.9后就可使用上面的方法创建工作簿,并可任意生成xls或xlsx文件而不损坏。

//1.创建工作簿HSSFWorkbook workbook = new HSSFWorkbook();//2.创建一个工作表--sheetHSSFSheet sheet =workbook.createSheet("自测sheet");

<!-- 03版本Excel导入 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency><!-- 07版本Excel导入 --><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><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope><version>4.12</version></dependency>

poi导入导出Excel是先将数据全部写入内存中,再进行后续操作。因此可能会导致内存泄漏。

简单的创建一个表格

String path="D:\\Work\\IdeaProject\\boot05-web-admin";//1.创建工作簿(03版为HSSFWorkbook,07版为XSSFWorkbook 但好像可以混用(待确认))Workbook workbook = new XSSFWorkbook();//2.创建一个工作表--sheetSheet sheet =workbook.createSheet("自测sheet");//3.创建一个行Row row=sheet.createRow(0);//4.创建一个单元格(1,1)Cell cell=row.createCell(0);cell.setCellValue("芜湖");//(1,2)Cell cell2=row.createCell(1);cell2.setCellValue("houbu");//创建第二行Row row2=sheet.createRow(1);Cell cell21=row2.createCell(0);cell21.setCellValue("起飞");Cell cell22=row2.createCell(1);cell22.setCellValue(2234);//生成一张表,使用07版本 使用xls结尾FileOutputStream fo=new FileOutputStream(path+"/ceshi.xlsx");workbook.write(fo);//关闭流fo.close();System.out.println("生成完毕");

Excel写入的几种方法

@Overridepublic void BigDataWrite03() throws IOException{//03版的Excel写入long begin = System.currentTimeMillis();Workbook wb=new HSSFWorkbook();Sheet sheet=wb.createSheet();for (int i = 0; i < 65536; i++) {Row row=sheet.createRow(i);for (int i1 = 0; i1 < 10; i1++) {Cell cell=row.createCell(i1);cell.setCellValue(i1);}}FileOutputStream fo=new FileOutputStream(path+"/bigExcel03.xls");wb.write(fo);fo.close();long end = System.currentTimeMillis();System.out.println((long)(end-begin)/1000);}@Overridepublic void BigDataWrite07() throws IOException {//07版方法速度慢,但可以写入更多数据long begin = System.currentTimeMillis();Workbook wb=new XSSFWorkbook();Sheet sheet=wb.createSheet();for (int i = 0; i < 100000; i++) {Row row=sheet.createRow(i);for (int i1 = 0; i1 < 10; i1++) {Cell cell=row.createCell(i1);cell.setCellValue(i1);}}FileOutputStream fo=new FileOutputStream(path+"/bigExcel07.xlsx");wb.write(fo);fo.close();long end = System.currentTimeMillis();System.out.println((long)(end-begin)/1000);}@Overridepublic void BigDataWrite07S() throws IOException {//SXSSFWorkbook类是更快的07版本操作类,但在操作时会产生临时文件,记得删除long begin = System.currentTimeMillis();Workbook wb=new SXSSFWorkbook();Sheet sheet=wb.createSheet();for (int i = 0; i < 100000; i++) {Row row=sheet.createRow(i);for (int i1 = 0; i1 < 10; i1++) {Cell cell=row.createCell(i1);cell.setCellValue(i1);}}FileOutputStream fo=new FileOutputStream(path+"/bigExcel07s.xlsx");wb.write(fo);//删除写入时产生的临时文件((SXSSFWorkbook)wb).dispose();fo.close();long end = System.currentTimeMillis();System.out.println((long)(end-begin)/1000);}

简单的读取Excel内容

public void simpleExcelRead() throws IOException {//通过io读取文件FileInputStream io=new FileInputStream("D:\\Work\\IdeaProject\\boot05-web-admin\\ceshi.xlsx");//创建一个07版工作簿,excel的操作它都可以操作Workbook wb=new XSSFWorkbook(io);//得到表Sheet sheet= wb.getSheetAt(0);//得到列Row row = sheet.getRow(0);//得到行Cell cell=row.getCell(0);System.out.println(cell.getStringCellValue());}

读取Excel中的内容并判断其数据类型

public void ExcelRead() throws IOException{FileInputStream io= new FileInputStream(path+"/Insert.xlsx");Workbook wb=new XSSFWorkbook(io);//获取第一个sheet页Sheet sheet=wb.getSheetAt(0);//获取第一行(一般是表头)Row rowTitle=sheet.getRow(0);if(rowTitle != null){//获取这行的cell数量int cellCount=rowTitle.getPhysicalNumberOfCells();//遍历获取表头内容for (int cellNum =0;cellNum<cellCount;cellNum++){Cell cell=rowTitle.getCell(cellNum);if(cell != null){int cellType=cell.getCellType();String value=cell.getStringCellValue();System.out.println(cellType+"==>"+value);}}//获取当前sheet页中的总行数int rowCount=sheet.getPhysicalNumberOfRows();//遍历所有行的内容for(int rowNum=1;rowNum<rowCount;rowNum++){//跳过表头,从第二行开始获取Row row=sheet.getRow(rowNum);if(row != null){//获取当前行的总列数cellCount=row.getPhysicalNumberOfCells();//遍历当前行的所有列的信息for (int cellNum=0;cellNum<cellCount;cellNum++){//拿取当前列Cell cell=row.getCell(cellNum);if(cell != null){//获取列中内容的类型int cellType=cell.getCellType();String cellValue="";//判断类型switch (cellType){case HSSFCell.CELL_TYPE_STRING://字符串cellValue=cell.getStringCellValue();System.out.println(cellNum+"这是字符串"+cellValue);break;case HSSFCell.CELL_TYPE_BOOLEAN://boolean类型cellValue=String.valueOf(cell.getBooleanCellValue());System.out.println(cellNum+"boolean类型"+cellValue);break;case HSSFCell.CELL_TYPE_NUMERIC://这是数字类型,但是包含数字和日期if (HSSFDateUtil.isCellDateFormatted(cell)){System.out.println("是日期");cellValue=String.valueOf(cell.getDateCellValue());}cellValue=String.valueOf(cell.getNumericCellValue());System.out.println(cellNum+"数字类型"+cellValue);break;case HSSFCell.CELL_TYPE_BLANK://为空break;case HSSFCell.CELL_TYPE_ERROR://错误System.out.println("数据类型错误");break;}}}}}}io.close();}

使用EasyExcel进行操作:

导入依赖

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.10</version></dependency>

详情可参照官方文档:EasyExcel · 语雀

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。