Java使用easyExcel操作Excel下载模板 亲测有效

 /**
     * 下载Excel
     *
     * @param response 请求response
     */
    @RequestMapping({"/template/download"})
    public static void downExcel(HttpServletResponse response) {
        InputStream fileInputStream = null;
        InputStream fis = null;
        OutputStream outputStream = null;
        String fileName = "科学数据平台数据集.xlsx";
        try {
            // 将文件写入输入流 
            //主要ClassPathResource是获取类路径下的文件
            fileInputStream = (new ClassPathResource("templates/" + fileName)).getInputStream();
            fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            // 解决跨域问题,这句话是关键,对任意的域都可以,如果需要安全,可以设置成安前的域名
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.setHeader("FileName", URLEncoder.encode(fileName, "UTF-8"));
            response.setHeader("Access-Control-Expose-Headers", "FileName");
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 告知浏览器文件的大小
           // response.addHeader("Content-Length", "" + fileInputStream.length());
            outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            ActionResult.fail("文件下载异常");
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

大题结构如图所示

即可完成模板下载!!!

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值