springboot+poi 导出excle文件

.根据接口文档开发。完成一个简单的导出功能

 

1.导入pom依赖


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

2.把获取数据的方法写好, 这里直接上service层代码

ServiceImpl

 @Override
    public List<VApiRequestDaily> exportDailyBill(Long merchantUserId, String productCode, Date startDate, Date endDate)
    {
        return vApiRequestDailyMapper.getListByMerchantUserId(merchantUserId, productCode,
                                                              MdDateUtils.intValueOfDay(startDate),
                                                              MdDateUtils.intValueOfDay(endDate));
    }

3.BizImpl

 @Override
    public Workbook exportDailyBill(Long merchantUserId, String productCode, Date startDate, Date endDate)
    {
        List<VApiRequestDaily> dailyList =
                merchantService.exportDailyBill(merchantUserId, productCode, startDate, endDate);
        // 创建excel工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建第一个sheet(页),并命名
        Sheet sheet = workbook.createSheet("日账单");
        //设置首行标题标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("统计日");
        row.createCell(1).setCellValue("请求次数");
        row.createCell(2).setCellValue("计费次数");
        row.createCell(3).setCellValue("计费金额");
        //新增数据行,并且设置单元格数据
        int rowNum = 1;
        for(VApiRequestDaily dailyBill : dailyList)
        {
            row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(formatIntDateVal(dailyBill.getStatsDay()));
            row.createCell(1).setCellValue(dailyBill.getRequestQty());
            row.createCell(2).setCellValue(dailyBill.getChargeAmt());
            row.createCell(3).setCellValue(dailyBill.getChargeAmt());
            rowNum++;
        }
        return workbook;
    }

Controller

   /**
     * 导出
     */
    @LoginRequired
    @GetMapping(value = "/user/{merchantUserId}/bill/export")
    public void getExcel(@PathVariable(value = "merchantUserId") Long merchantUserId,
            @RequestParam(value = "productCode") String productCode, @RequestParam(value = "startDate") Date startDate,
            @RequestParam(value = "endDate") Date endDate, HttpServletResponse response) throws Exception
    {
        Workbook workbook = merchantBiz.exportDailyBill(merchantUserId, productCode.toUpperCase(), startDate, endDate);
        response.setHeader("Content-type", "application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
        workbook.write(response.getOutputStream());
        workbook.close();
    }

用postman测试

成功

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值