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

苍穹外卖学习笔记(三十二最终篇)

Apache POI

介绍

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

一般情况下,POI都是用于操作Excel文件

应用场景:

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

入门案例

1. 导入maven坐标

 <!-- 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>

2. POITest.java

package com.sky.test;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author Jie.
 * @description: TODO
 * @date 2024/10/22
 * @version: 1.0
 */
public class POITest {

    //通过POI创建Excel文件并且写入文件内容
    public static void write() throws Exception{
        //在内存中创建Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //在Excel中创建一个Sheet页
        XSSFSheet sheet = excel.createSheet("info");
        //在Sheet中创建行对象,rownum从0开始
        XSSFRow row = sheet.createRow(0);
        //在每一行上创建单元格
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("城市");

        row = sheet.createRow(1);

        row.createCell(0).setCellValue("张三");
        row.createCell(1).setCellValue("北京");

        row = sheet.createRow(2);

        row.createCell(0).setCellValue("李四");
        row.createCell(1).setCellValue("天津");

        FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx"));
        excel.write(out);

        // 关闭资源
        excel.close();
        out.close();
    }

    //通过POI读取Excel文件并且读入文件内容
    public static void read() throws Exception{
        //读取磁盘上已经存在的excel文件
        FileInputStream fileInputStream = new FileInputStream("D:\\info.xlsx");
        XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheet = excel.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 1;i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String name1 = row.getCell(1).getStringCellValue();
            System.out.println(name + " " + name1);
        }
    }

    public static void main(String[] args) throws Exception{
        write();
        read();
    }
}

导出运营数据Excel报表

ReportController

    @GetMapping("/export")
    @ApiOperation("导出Excel报表")
    public void export(HttpServletResponse response) {
        reportService.exportBusinessData(response);
    }

ReportService

    /**
     * 导出
     * @param response 响应
     */
    void exportBusinessData(HttpServletResponse response);

ReportServiceImpl

/**
     * 导出
     *
     * @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);
            //填充营业额数据
            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);

            //4. 关闭资源
            out.close();
            excel.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

❤️❤️❤️完结撒花❤️❤️❤️


http://www.kler.cn/news/361264.html

相关文章:

  • 构建高效智慧社区:Spring Boot Web框架应用
  • Ubuntu配置FTP
  • 基于图像拼接开题报告
  • Python 正则
  • Prompt提示词设计:如何让你的AI对话更智能?
  • EasyExcel自定义下拉注解的三种实现方式
  • 容灾与云计算概念
  • 加密DNS有什么用?
  • 网络安全——防火墙技术
  • 在 Kylin Linux 上安装 PostgreSQL 以下是安装 PostgreSQL 的步骤:
  • linux命令基础
  • 边缘计算网关兼容多种通信协议实现不同设备和系统互联互通
  • python实战项目46:selenium爬取百度新闻
  • 应急响应:ARP欺骗实战
  • [数据集][目标检测]电力场景输电线路巡检检测数据集VOC+YOLO格式8667张50类别
  • 常见Elasticsearch 面试题答案详细解析(下)
  • 【matlab代码】无迹粒子滤波(Unscented Particle Filter)例程,一维直线上的滤波,状态量为位置和速度、观测量为位置
  • 高级SQL技巧:掌握数据分析与优化的艺术
  • 代码随想录训练营第66天|Floyd
  • UNIAPP弹窗跳转页面无法滚动bug