easyexcel导出excel 怎么写回浏览器
时间: 2023-07-21 19:21:42 浏览: 236
要将 EasyExcel 导出的 Excel 文件写回到浏览器,可以使用 Spring MVC 中的 HttpServletResponse 对象,具体操作如下:
1. 在 Controller 中定义导出 Excel 的方法,使用 EasyExcel 将数据写入 OutputStream 中:
```
@GetMapping("/export")
public void exportExcel(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("导出数据.xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
List<DemoData> data = getData();
OutputStream out = response.getOutputStream();
ExcelWriter writer = EasyExcel.write(out, DemoData.class).build();
Sheet sheet = new Sheet(1, 0, DemoData.class);
writer.write(data, sheet);
writer.finish();
out.flush();
}
```
2. 在方法中设置 HttpServletResponse 的响应头,告诉浏览器下载文件,同时设置文件名为“导出数据.xlsx”。
3. 使用 EasyExcel 的 ExcelWriter 写入数据到 OutputStream 中。
4. 调用 HttpServletResponse 的 getOutputStream 方法获取输出流,将写入的数据通过输出流返回到浏览器。
注意:需要在 pom.xml 文件中引入 easyexcel 的依赖:
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)