使用easypoi导出Excel

1.导入依赖

<!--Easy poi -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.1</version>
        </dependency>

跑通例子

@GetMapping("/exportIncomeChart")
    @ApiOperation(value = "导出后台总览收入图表")
    public void exportIncomeChart(@RequestParam(required = false) Integer source, @RequestParam(required = false) String starDate, @RequestParam(required = false) String endDate, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Date date1 = new Date(Long.parseLong(starDate)*1000);   //对应的就是时间戳对应的Date
        Date date2 = new Date(Long.parseLong(endDate)*1000);   //对应的就是时间戳对应的Date

        Request<ManageIncome> request2 = new Request<>();
        ManageIncome income = new ManageIncome();
        if (StringUtil.isBlank(starDate)||StringUtil.isBlank(endDate)){
            date2=new Date();
            date1=couOrderService.getPastDate(7, new Date());
        }
        income.setStarDate(date1);
        income.setEndDate(date2);
        income.setSource(source);
        request2.setData(income);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        List<ManageIncomeExport> exports = couOrderService.incomeChart(request2);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("收入明细表","明细"),
                ManageIncomeExport.class, exports);
        flushWorkBook(request,response,workbook,sdf.format(income.getEndDate())+"-"+sdf.format(income.getStarDate())+"收入明细表");
    }

    public static void flushWorkBook(HttpServletRequest request, HttpServletResponse response,
                                     Workbook wb, String excelName) {
        try {
            String attachFilename =excelName+ ".xlsx";// 文件名
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msexcel;charset=utf-8");
            String agent = request.getHeader("USER-AGENT");//
            String encodeFileName = null;
            if (null != agent
                    && (-1 != agent.indexOf("MSIE") || -1 != agent.indexOf("Trident") || -1 != agent
                    .indexOf("Edge"))) {// ie
                encodeFileName = new String(attachFilename.getBytes("utf-8"), "ISO8859-1");// ISO8859-1
            } else {
                encodeFileName = java.net.URLEncoder.encode(attachFilename, "UTF8");
            }
            response.setHeader("Content-Disposition", "attachment;filename=\"" + encodeFileName
                    + "\"");
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                }
            }
        }
    }

实体类注释



/**
 * <p>
 * 
 * </p>
 *
 * @author tujr
 * @since 2020-08-26
 */
@Data
public class ManageIncomeExport {

    @ApiModelProperty(value = "线上课程拼团")
    Double onGroupCourse;
    @ApiModelProperty(value = "线下课程拼团")
    Double donGroupCourse;

    @Excel(name = "线上课程", height = 20, width = 30, isImportField = "true_st")
    @ApiModelProperty(value = "线上课程")
    Double onCourse;

    @Excel(name = "线下课程", height = 20, width = 30, isImportField = "true_st")
    @ApiModelProperty(value = "线下课程")
    Double donCourse;

    @ApiModelProperty(value = "拼团收入")
    Double groupCount;

    @Excel(name = "总收入", height = 20, width = 30, isImportField = "true_st")
    @ApiModelProperty(value = "总收入")
    Double count;

    @Excel(name = "来源", height = 20, width = 30, isImportField = "true_st",
            replace = {"手机端_0","PC端_1","全部_2"})
    @ApiModelProperty(value = "来源0手机端 1PC端 2全部")
    Integer source;

    @Excel(name = "下单时间", height = 20, width = 30, isImportField = "true_st",format="yyyy-MM-dd")
    @ApiModelProperty(value = "时间")
    Date time;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

默语玄

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

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

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

打赏作者

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

抵扣说明:

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

余额充值