当前位置: 首页 > news >正文

Apache POI

Apache POI 是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用 POI 在 Java 程序中对Miscrosoft Office各种文件进行读写操作。 一般情况下,POI 都是用于操作 Excel 文件。

业务场景:导入Excel文件内数据到数据库内、把数据库内的数据导出为Excel报表。

maven坐标

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

将数据写入Excel文件示例

@Testpublic void testApachePOI(){//在内存中创建一个Excel对象XSSFWorkbook excel = new XSSFWorkbook();//创建Shell页XSSFSheet sheet = excel.createSheet("itcast");//在Sheet页中创建行,0为第一行,关于ApachePOI的行和列都是从0开始XSSFRow row = sheet.createRow(0);//创建单元格,0为第一列row.createCell(0).setCellValue("编号");row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("年龄");//创建数据行XSSFRow dataRow = sheet.createRow(1);dataRow.createCell(0).setCellValue(1);dataRow.createCell(1).setCellValue("张三");dataRow.createCell(2).setCellValue(18);//创建数据行XSSFRow dataRow2 = sheet.createRow(2);dataRow2.createCell(0).setCellValue(2);dataRow2.createCell(1).setCellValue("张四");dataRow2.createCell(2).setCellValue(19);//创建输出流,指定路径FileOutputStream out = null;try {out = new FileOutputStream("D:\\excel\\test.xlsx");excel.write(out);}catch (Exception e){e.printStackTrace();}finally {if (excel != null){try {excel.close();} catch (Exception e) {e.printStackTrace();}}if (out != null){try {out.close();} catch (Exception e) {e.printStackTrace();}}}}

文档

读取excel的数据

@Testpublic void testApachePOI2() throws Exception{FileInputStream in = new FileInputStream("D:\\excel\\test.xlsx");//通过输入流读取指定的文件XSSFWorkbook excel = new XSSFWorkbook(in);//获取第一个Sheet页XSSFSheet sheet = excel.getSheetAt(0);int LastRowNum = sheet.getLastRowNum();for (int i = 0; i <= LastRowNum; i++) {XSSFRow row = sheet.getRow(i);if (i==0){System.out.println(row.getCell(0).getStringCellValue()+","+row.getCell(1).getStringCellValue()+","+row.getCell(2).getStringCellValue());}else {System.out.println(row.getCell(0)+","+row.getCell(1).getStringCellValue()+","+row.getCell(2));}}in.close();excel.close();}

实战:在用报表模板创建三十天营业额统计

Controller层

 @GetMapping("/export")@ApiOperation("导出数据")public Result exportData(HttpServletResponse  response){log.info("开始导出数据...");reportService.exportData(response);return null;}

Service层

  /*** 导出近30天的运营数据报表* @param response*/@Overridepublic void exportData(HttpServletResponse response) {LocalDate now = LocalDate.now();LocalDate begin = now.minusDays(30);LocalDate end = now.minusDays(1);//查询运营数据BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN),LocalDateTime.of(end, LocalTime.MAX));//读取模板,将其作为输入流读取到内存中InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");try {XSSFWorkbook excel = new XSSFWorkbook(inputStream);XSSFSheet sheet = excel.getSheet("Sheet1");sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);//获取第四行XSSFRow row4 = sheet.getRow(3);row4.getCell(2).setCellValue(businessDataVO.getTurnover());row4.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row4.getCell(6).setCellValue(businessDataVO.getNewUsers());//获取第五行XSSFRow row5 = sheet.getRow(4);row5.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row5.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));XSSFRow row = sheet.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());}//通过输出流进行文件下载到客户端服务器中OutputStream outputStream = response.getOutputStream();excel.write(outputStream);outputStream.close();excel.close();inputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}

模板放在

所以可以通过获取类->获取类加载器->获取resource->路径获得。

BusinessDataVO

package com.sky.vo;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;/*** 数据概览*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BusinessDataVO implements Serializable {private Double turnover;//营业额private Integer validOrderCount;//有效订单数private Double orderCompletionRate;//订单完成率private Double unitPrice;//平均客单价private Integer newUsers;//新增用户数}

模板

导出示例

补充:

HttpServletResponse 属于 Java Servlet API 框架的一部分。
框架介绍
所属框架: Java Servlet API(Java EE/Web容器标准API)
作用: 用于处理HTTP响应的核心接口
相关方法说明
在当前代码中使用的 HttpServletResponse 方法包括:
getOutputStream() - 获取响应的输出流,用于向客户端发送二进制数据(如Excel文件)
setContentType() - 设置响应内容类型(MIME类型)
setHeader() - 设置HTTP响应头信息

http://www.dtcms.com/a/602053.html

相关文章:

  • 某景区网站建设策划书利用codeing做网站
  • rhce作业
  • 网页网站原型图占位符怎么做定制一个微信小程序要多少钱
  • Python-PLAXIS自动化建模技术与典型岩土工程案例
  • 4-ARM-PEG-Fmoc protected Amine(2),合成设计思路与路线选择
  • 自主可控背景下MCU芯片的替代之路:从ARM到RISC-V的机遇与挑战
  • 想建设个网站全国最大的网站建设公司
  • 做网站的编程语言组合江西建设厅特殊工种的网站
  • HDPlanner 代码阅读
  • AOSP Android13 Launcher3 最近任务详解
  • 青岛市网站制作seo搜索引擎优化薪资
  • MediaPipe LLM Inference:在WEB浏览器中“裸跑”大语言模型
  • 网站平台建设公司经营范围域名注册成功怎么做网站
  • 南昌哪里有网站建设网页制作平台flash
  • 2025 创客匠人全球创始人 IP + AI 万人高峰论坛:家庭教育与企业管理的变革指南
  • Canvas指纹模拟避坑指南Canvas指纹防护实测案例
  • 2.11 实践二:基于 LoRA 微调一个垂直领域客服问答模型并部署为 API
  • 建设房屋出租网站饮食网站首页页面
  • 网站怎样自动文字排版网站建设58
  • 从工作流搭建看智能体与RPA流程自动化有何不同?
  • C语言编译器IDE | 提升程序开发效率的最佳选择
  • 当遇到 502 错误(Bad Gateway)怎么办
  • 告别停机焦虑:耐达讯自动化Profibus光纤模块——您的控制链路‘救星’在此”
  • 天津做网站优化的公司酒店网站收入如何做帐务处理
  • 数据智能时代的安全困局与 AI 破局逻辑
  • Docker镜像操作:构建、推送、拉取与优化
  • 网站流量怎么做的丹阳网站建设哪家好
  • 做团餐 承包食堂的企业网站管理咨询项目
  • 什么是大数据迁移?
  • Paimon——追根溯源