java使用EasyExcel模板导出并下载

导语

对于类似这种的不规则excel表格导出,可以使用阿里的EasyExcel开源工具中的模板导出功能实现,只需要在excel文件对应的单元格中挖空,就可以根据任意类中字段的名称自动填入内容到对应的空中,非常方便

正片

1:引入依赖

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>3.3.2</version>
            </dependency>

2:Controller接口

    @PostMapping("/export/{id}")
    @ApiOperation(value = "导出")
    public void export(@PathVariable(value = "id") Long id,HttpServletResponse response) {
        service.export(id,response);
    }

3:Service


    @Override
    public void export(Long id , HttpServletResponse response){
        //模板路径
        String path="/data/aaa.xlsx";
        //查实体
        AppealVO appeal = getOneDetails(id);
        //导出
        templateExport(path, "目标文件名",appeal,response);
    }

    @SneakyThrows
    public static void templateExport(String templatePath,String fileName,Object input, HttpServletResponse response) {
        String suffix=".xlsx";
        if (!fileName.contains(suffix)) {
            fileName += suffix ;
        }
        //设置下载文件名
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        if (ObjectUtil.isNotEmpty(fileName)) {
            fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName  + "\"; filename*=utf-8''" + fileName);
        }
        response.setCharacterEncoding("utf-8");

        // sheet:第0个
        EasyExcel.write(response.getOutputStream()).withTemplate(templatePath).sheet(0).doFill(input);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件时,如何设置单元格样式? A:使用easyexcel模板导出Excel文件时,可以通过设置注解@ExcelProperty的style属性来指定对应单元格的样式。具体操作如下: 1. 定义样式类,继承自com.alibaba.excel.write.style.AbstractCellStyleStrategy,重写setCellStyle方法,对单元格样式进行设置。示例代码如下: ``` public class CustomCellStyleStrategy extends AbstractCellStyleStrategy { private CellStyle cellStyle; public CustomCellStyleStrategy(Workbook workbook) { this.cellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontName("微软雅黑"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); } @Override protected CellStyle setCellStyle(Cell cell, Head head, Integer integer, Integer integer1) { return cellStyle; } } ``` 2. 在需要导出的实体类中,为需要定制样式的属性添加@ExcelProperty注解,设置style属性为对应的样式类。示例代码如下: ``` public class User { @ExcelProperty(value = "姓名", index = 0, style = CustomCellStyleStrategy.class) private String name; @ExcelProperty(value = "年龄", index = 1) private Integer age; // ... } ``` 在注解中,value属性指定了单元格的标题,index属性指定了导出的顺序,style属性指定了样式类。 通过以上步骤,即可设置单元格样式。需要注意的是,样式类需要在导出Excel时传入,如下示例代码: ``` List<User> userList = new ArrayList<>(); // ... 添加用户数据 // 构造导出参数对象 WriteWorkbook writeWorkbook = EasyExcel.write(fileName).build(); WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").head(User.class).registerWriteHandler(new CustomCellStyleStrategy(writeWorkbook.getWorkbook())).build(); // 执行导出 EasyExcel.write(fileName, User.class).sheet("Sheet1").build().write(userList); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值