ssm 商城实战 文件导出为execl 格式

文件导入execl 文件的步骤:
第一步:引入依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.1</version> </dependency
第二步:Java代码
注意事项:
1、需要设置文件的格式:
produces = "application/vnd.ms-excel"
2、需要设置导出的格式:
fileName = URLEncoder.encode(fileName, "UTF-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
3、handler 属性是二级缓存代理添加的 如果没有开启二级缓存,可以不用此项。
@RequestMapping(value = "/excel", produces = "application/vnd.ms-excel") public void excel(HttpServletResponse resp) throws IOException { List<Order> orderList = os.findAll(); String fileName = "学生数据表-" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + ".xlsx"; //导出格式 ,防止乱码 fileName = URLEncoder.encode(fileName, "UTF-8"); // 设置响应头,用于指定下载时的文件名 resp.setHeader("Content-Disposition", "attachment;filename=" + fileName); // handler属性是由于mybatis的延迟加载创建的模型类的代理的一个属性,必须排除 EasyExcel.write(resp.getOutputStream(), Order.class) .sheet("学生数据表") //二级缓存会多一个变量 此方法排除 .excludeColumnFieldNames(List.of("handler")) .doWrite(orderList); }
作者:域名博客,网站地址:https://liuguangfa.com/,转载注明出处!