欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > Apache POI实现Excel的基本写入、导出操作

Apache POI实现Excel的基本写入、导出操作

2025/5/7 16:36:44 来源:https://blog.csdn.net/equila/article/details/147688738  浏览:    关键词:Apache POI实现Excel的基本写入、导出操作

目录

一、Apache POI 简介

二、入门案例(写入导出)

三、实际开发过程中的导出操作——(将文件下载至客户端浏览器中)


一、Apache POI 简介

Apache POI(Poor Obfuscation Implementation)是 Apache 软件基金会的开源项目,提供 Java 操作 Microsoft Office 格式文件(如 Excel、Word、PowerPoint 等)的 API 库。

二、入门案例(写入导出)

1.导入Maven坐标

<!-- poi -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>

2.创建excel文件,并写入数据

    /*** 通过POI创建Excel文件并写入数据*/public static void write() throws IOException {// 在内存中创建一个新的Excel文件XSSFWorkbook excel = new XSSFWorkbook();// 创建一个新的sheet(sheet名为“info”)XSSFSheet sheet = excel.createSheet("info");// 在sheet中创建行对象,rowNum表示行号(从0开始)XSSFRow row = sheet.createRow(1);//创建单元格,并写入数据row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("城市");
​//创建一个新行row = sheet.createRow(2);//创建单元格,并写入数据row.createCell(1).setCellValue("张三");row.createCell(2).setCellValue("北京");
​row = sheet.createRow(3);row.createCell(1).setCellValue("李四");row.createCell(2).setCellValue("南京");
​// 通过输出流将内存中的Excel文件写入到磁盘中FileOutputStream out = new FileOutputStream("D:\\info.xlsx");excel.write(out);
​// 关闭资源out.close();excel.close();}

效果图:

3.读取excel文件中的内容

    /*** 通过POI读取Excel文件中的数据*/public static void read() throws IOException {// 创建输入流FileInputStream in = new FileInputStream("D:\\info.xlsx");// 通过输入流读取Excel文件XSSFWorkbook workbook = new XSSFWorkbook(in);// 获取第一个sheetXSSFSheet sheet = workbook.getSheetAt(0);
​int lastRowNum = sheet.getLastRowNum();for(int i=1;i<=lastRowNum;i++){// 获取第一行XSSFRow row = sheet.getRow(i);// 获取单元格的值String cellValue1 = row.getCell(1).getStringCellValue();String cellValue2 = row.getCell(2).getStringCellValue();System.out.println(cellValue1 +"  "+ cellValue2);}// 关闭资源in.close();workbook.close();}

效果图:

三、实际开发过程中的导出操作——(将文件下载至客户端浏览器中)

1.准备好模板excel文件(调格式会比较麻烦,因此直接准备好)放到resources下的template文件夹下。

 2.查询数据,将数据写入至excel文件,并生成输出流供客户端下载。

代码示例:

/*** 导出运营数据报表*/
@Override
public void exportBusinessData(HttpServletResponse response) {//1.查询数据库,获取营业数据---查询最近30天的运营数据LocalDate begin = LocalDate.now().minusDays(30);LocalDate end = LocalDate.now().minusDays(1);//查询概览数据BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));//2.通过POI将数据写入Excel文件InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");try {//基于模板文件创建一个新的Excel文件XSSFWorkbook excel = new XSSFWorkbook(in);//获取表格文件的Sheet标签页XSSFSheet sheet1 = excel.getSheet("Sheet1");//填充数据--时间sheet1.getRow(1).getCell(1).setCellValue("时间:"+ begin + "至" + end);//获得第四行XSSFRow row = sheet1.getRow(3);row.getCell(2).setCellValue(businessDataVO.getTurnover());row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row.getCell(6).setCellValue(businessDataVO.getNewUsers());//获得第五行row = sheet1.getRow(4);row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getUnitPrice());//填充明细数据for(int i=0;i<30;i++){LocalDate date = begin.plusDays(i);//查询某一天的营业数据businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));//获得某一行row = sheet1.getRow(i+7);row.getCell(1).setCellValue(date.toString());row.getCell(2).setCellValue(businessDataVO.getTurnover());row.getCell(3).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row.getCell(5).setCellValue(businessDataVO.getUnitPrice());row.getCell(6).setCellValue(businessDataVO.getNewUsers());}//3.通过输出流将Excel文件下载到客户端浏览器ServletOutputStream out= response.getOutputStream();excel.write(out);//关闭资源out.close();excel.close();in.close();} catch (IOException e) {throw new RuntimeException(e);}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词