Apache POl的使用(导出报表)

介绍

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

Apache POl的应用场景:

  • 银行网银系统导出交易明细
  • 各种业务系统导出Excel报表
  • 批量导入业务数据

导入maven坐标

XML 复制代码
   <!-- poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi}</version>
            </dependency>

写操作

java 复制代码
    /**
     * 通过POI创建Excle文件
     */
    @Test
    public void write() throws Exception{
        //在内存中创建一个excle文件
        XSSFWorkbook excel=new XSSFWorkbook();
        //在excel文件中创建一个sheet页
        XSSFSheet sheet = excel.createSheet("info");
        //创建行对象,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("杭州");

       FileOutputStream out= new FileOutputStream(new File("D:\\BaiduNetdiskDownload\\cangqiong\\ziliao\\ziliao\\day12\\test.xlsx"));
        excel.write(out);
        //关闭资源
        out.close();
        excel.close();
    }

读操作

java 复制代码
@Test
    public void read() throws Exception{
        //输入流,读取磁盘上已经存在的excle文件
        InputStream in=new FileInputStream(new File("D:\\BaiduNetdiskDownload\\cangqiong\\ziliao\\ziliao\\day12\\test.xlsx"));
        XSSFWorkbook excle =new XSSFWorkbook(in);
        //读取文件中第一个sheet页
        XSSFSheet sheet = excle.getSheetAt(0);
        //获得sheet中最后一行行号
        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();
            out.println(cellValue1+""+cellValue2);
        }
        //关闭资源
        in.close();
        excle.close();
    }

代码开发

实现步骤:

  1. 设计Excel模板文件
  2. 查询近30天的运营数据
  3. 将查询到的运营数据写入模板文件
  4. 通过输出流将Excel文件下载到客户端浏览器

ReportController

java 复制代码
    /**
     * 报表统计
     *
     */
    @GetMapping("/export")
    @ApiOperation("导出运营数据报表")
    public void export(HttpServletResponse response){
       reportService.exportBusinessData(response);
    }

ServiceImpl

java 复制代码
    /**
     * 导出运营数据报表
     *
     * @param response
     */
    @Override
    public void exportBusinessData(HttpServletResponse response) {
        //1查询数据库,获取营业数据
        LocalDate dateBegin = LocalDate.now().minusDays(30);
        //前一天
        LocalDate dateEnd = LocalDate.now().minusDays(1);
        //查询概览数据
        BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));

        //2.通过poi将数据写入到excel中
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
        //基于模板文件创建一个新的文件
        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);
            //获取表格文件sheet标签页
            XSSFSheet sheet = excel.getSheet("Sheet1");
            //填充数据--时间
            sheet.getRow(1).getCell(1).setCellValue("时间:" + dateBegin + "至" + dateEnd);
            XSSFRow row = sheet.getRow(3);
            row.getCell(2).setCellValue(businessDataVO.getTurnover());
            row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
            row.getCell(6).setCellValue(businessDataVO.getNewUsers());
            //获得第五行
            row = sheet.getRow(4);
            row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
            row.getCell(4).setCellValue(businessDataVO.getUnitPrice());
            //填充明细数据
            for (int i=0;i<30;i++){
                LocalDate date=dateBegin.plusDays(i);
                BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
                row= sheet.getRow(7+i);
                row.getCell(1).setCellValue(date.toString());
                row.getCell(2).setCellValue(businessData.getTurnover());
                row.getCell(3).setCellValue(businessData.getValidOrderCount());
                row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
                row.getCell(5).setCellValue(businessData.getUnitPrice());
                row.getCell(6).setCellValue(businessData.getNewUsers());
            }

            //3通过输出流将excel文件下载到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            excel.write(out);
            //关闭资源
            out.close();
            excel.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

完结!

相关推荐
крон26 分钟前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan1 小时前
c++ 单例模式
开发语言·c++·单例模式
coderSong25681 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
老胖闲聊1 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1181 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
Mr_Air_Boy2 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
曹勖之2 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?2 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头3 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
年老体衰按不动键盘3 小时前
快速部署和启动Vue3项目
java·javascript·vue