Spring Boot+Apache POI实战:定时任务数据高效导出Excel方案全解析

首先

在生成Excel前,我们需要理解一下Excel文件的组织形式。在POI中,是这样理解的:一个Excel文件对应一个workbook,一个workerbook是有若干个sheet组成的。一个sheet有多个row,一个row一般存在多个cell。

1、建立所需要的导出的数据库

2、通过插件生成对应的实体类以及CRUD的方法

/**
 * TbHkhb:航空航班表
 * @author zyw
 * @since 2022-03-18 10:31:24
 */
@Data
@ApiModel(value="航空航班表,对应表tb_hkhb",description="适用于新增和修改页面传参")
public class TbHkhb  extends ProBaseEntity<TbHkhb>  {

    private static final long serialVersionUID = 1L;
    /**
     * 主键ID
     */
    @ApiModelProperty(value="id")
    private String id;
    /**
     * 航班时间
     */
    @ApiModelProperty(value="航班时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private java.util.Date hbsj;
    /**
     * 出发地点
     */
    @ApiModelProperty(value="出发地点")
    private String cfdd;
    /**
     * 终点站
     */
    @ApiModelProperty(value="终点站")
    private String zdz;
    /**
     * 航班号
     */
    @ApiModelProperty(value="航班号")
    private String hbh;
    /**
     * 航空公司
     */
    @ApiModelProperty(value="航空公司")
    private String hkgs;
    /**
     * 乘客数量
     */
    @ApiModelProperty(value="乘客数量")
    private String cksl;
    /**
     * 座位数量
     */
    @ApiModelProperty(value="座位数量")
    private String zwsl;


}

3、导入POI依赖

        <!--使用POI读取文件-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

4、编写创建Excle文件的业务逻辑

    /**
     * @方法名称: createExcle
     * @实现功能: 导出值班值守表Excel文件 TODO: 方法入参根据页面对象设置
     * @return  java.lang.String
     * @create by zyw at 2022-03-21 15:20:31
     **/
    public String createExcle(){
        //第一步创建workbook
        HSSFWorkbook wb = new HSSFWorkbook();

        //第二步创建sheet
        HSSFSheet sheet = wb.createSheet("身份证错误信息");

        //第三步创建行row:添加表头0行
        //4个参数依次为:开始行,结束行,开始列,结束列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
        HSSFRow row = sheet.createRow(0);

        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("航空数据采集表");

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  //居中

        HSSFRow row1 = sheet.createRow(1);

        //第四步创建单元格
        HSSFCell cell = row1.createCell(0);         //第一个单元格
        cell.setCellValue("航班时间");                  //设定值
        cell.setCellStyle(style);                   //内容居中

        cell = row1.createCell(1);                   //第二个单元格
        cell.setCellValue("出发站点");
        cell.setCellStyle(style);

        cell = row1.createCell(2);                   //第三个单元格
        cell.setCellValue("终点站");
        cell.setCellStyle(style);

        cell = row1.createCell(3);                   //第四个单元格
        cell.setCellValue("航班号");
        cell.setCellStyle(style);

        cell = row1.createCell(4);                   //第五个单元格
        cell.setCellValue("航空公司");
        cell.setCellStyle(style);

        cell = row1.createCell(5);                   //第六个单元格
        cell.setCellValue("乘客数量");
        cell.setCellStyle(style);

        cell = row1.createCell(6);                   //第七个单元格
        cell.setCellValue("座位数");
        cell.setCellStyle(style);

        //第五步插入数据
        TbHkhb tbHkhb = new TbHkhb();
        List<TbHkhb> list = tbHkhbDao.findList(tbHkhb);
        for (int i = 0; i < list.size(); i++) {
            //创建行
            row = sheet.createRow(i+2);
            //创建单元格并且添加数据
            row.createCell(0).setCellValue(simpleDateFormat.format(list.get(i).getHbsj()));
            row.createCell(1).setCellValue(list.get(i).getCfdd());
            row.createCell(2).setCellValue(list.get(i).getZdz());
            row.createCell(3).setCellValue(list.get(i).getHbh());
            row.createCell(4).setCellValue(list.get(i).getHkgs());
            row.createCell(5).setCellValue(list.get(i).getCksl());
            row.createCell(6).setCellValue(list.get(i).getZwsl());
        }

        //第六步将生成excel文件保存到指定路径下
        try {
            File file = new File("C:/Users/asus/Desktop/航空数据采集表.xls");
            if (!file.exists()){
                FileOutputStream fout = new FileOutputStream("C:/Users/asus/Desktop/航空数据采集表.xls");
                wb.write(fout);
                fout.close();
            }else {
                FileOutputStream fout = new FileOutputStream("C:/Users/asus/Desktop/航空数据采集表(1).xls");
                wb.write(fout);
                fout.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return  "Excel文件生成成功...";
        }

    }
    /**
     * @方法名称: createExcle
     * @实现功能: 导出航空航班表Excel文件 TODO: 方法入参根据页面对象设置
     * @return  java.lang.String
     * @create by zyw at 2022-03-21 16:10:31
     **/
    @ApiOperation(value="导出航空航班表Excel文件",notes="返回Excel模板下载地址",response = String.class)
    @PostMapping(value = "/createExcle")
    public String createExcle(){
        try {
            return buildResultStr(buildSuccessResultData(service.createExcle()));
        }catch (RuntimeException e){
            logError(log, e);
            return buildResultStr(buildErrorResultData(e));
        }
    }

5、通过Swagger测试运行效果

数据库 :

Excle文件:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柚几哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值