导出excel表格并返回给前端的快捷用法(上)

这个比poi好用
先导入三个依赖

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>4.1.0</version>
</dependency>

然后给需要导出的的内容加注解

Excel class
 /**
     * ID
     */

    private String id;

    /**
     * 归属分类
     */
    @Excel(name = "归属分类",replace = {"住_1","会_2","食_3","康_4","其他_99"},isImportField = "true")
    private String classify;

    /**
     * 内容
     */
    @Excel(name = "内容",isImportField = "true")
    private String context;

最后在controller层加入

@ApiOperation("反馈 -导出")
    @PostMapping("exportExcel")
    public void exportExcel(@RequestBody @Valid ExcelDto dto, HttpServletResponse response) throws IOException {
        List<Excel> excels = ExcelService.selectExcel(dto);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("意见反馈","意见反馈"),
               Excel.class, excels);
        FileExportUtils.ExcelExport(response,workbook,"意见反馈.xls");
    }

FileExportUtils这是个返回给前台的方法,下篇文章会讲

你可能感兴趣的:(技能使用,代码相关知识)