使用easyexcel从浏览器下载excel

 2023-09-05 阅读 388 评论 0

摘要:添加依赖:得多加 两个依赖不然会报错误(亲身经历) 如果只加easyexcel依赖会报错误,解决:解决org.objectweb.asm.Type 如果继续报错class net.sf.cglib.core.DebuggingClassWriter has interface,解决办法:解决class net.sf.

添加依赖:得多加 两个依赖不然会报错误(亲身经历)
如果只加easyexcel依赖会报错误,解决:解决org.objectweb.asm.Type
如果继续报错class net.sf.cglib.core.DebuggingClassWriter has interface,解决办法:解决class net.sf.cglib.core.DebuggingClassWriter has interface

	<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.10</version><exclusions><exclusion><groupId>cglib</groupId><artifactId>cglib</artifactId></exclusion></exclusions></dependency><dependency><groupId>asm</groupId><artifactId>asm</artifactId><version>3.3.1</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2</version></dependency>

下载的代码:

@GetMapping("/testDownLoad")public void testRespExcel(HttpServletResponse response){String fileName = new String("导出excel.xlsx".getBytes(), StandardCharsets.ISO_8859_1);response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf8");response.setHeader("Content-disposition", "attachment;filename=" + fileName );try {
//            从HttpServletResponse中获取OutputStream输出流ServletOutputStream outputStream = response.getOutputStream();/** EasyExcel 有多个不同的read方法,适用于多种需求* 这里调用EasyExcel中通过OutputStream流方式输出Excel的write方法* 它会返回一个ExcelWriterBuilder类型的返回值* ExcelWriterBuilde中有一个doWrite方法,会输出数据到设置的Sheet中*/EasyExcel.write(outputStream, OcrFileInfoDownLoad.class).sheet("Sheet1").doWrite(employeeService.queryDownLoad());} catch (IOException e) {e.printStackTrace();}}

EmployeeService 查询的代码:

    public List<OcrFileInfoDownLoad> queryDownLoad() {return ocrFileInfoMapper.queryDownTest();}

OcrFileInfoDownLoad类:

@Data
@Builder
public class OcrFileInfoDownLoad {@ExcelProperty(value = "一级",index = 0)private String itemFirstCateName;@ExcelProperty(value = "二级",index = 1)private String itemSecondCateName;@ExcelProperty(value = "三级",index = 2)private String itemThirdCateName;}

下载下来的图片:
在这里插入图片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/18.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息