EasyExcel.write(response.getOutputStream(), SpcMasterData.class).sheet("模板").doWrite(list);
时间: 2024-05-25 16:12:25 浏览: 20
这段代码使用的是阿里巴巴的 EasyExcel 插件,用于将 Java 对象写入 Excel 文件,然后通过 response 输出到前端。其中,第一个参数为 response.getOutputStream(),表示将生成的 Excel 文件直接输出到前端,第二个参数为 SpcMasterData.class,表示要写入的 Java 对象的类型,第三个参数为 "模板",表示将要生成的 Excel 文件的 sheet 名称,第四个参数为 list,表示要写入的 Java 对象集合。最后,使用 doWrite() 方法将 Java 对象集合写入到 Excel 文件中。
相关问题
response.setContentType("application/vnd.ms-excel;charset=utf-8"); String fileName = URLEncoder.encode(startTime+"至"+endTime+"质量事故数据表", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), User.class) .autoCloseStream(Boolean.FALSE) .sheet("质量事故数据表") .doWrite(listAll);
这段代码用于在Java Web应用中生成并下载Excel文件。
首先,通过`response.setContentType("application/vnd.ms-excel;charset=utf-8")`设置响应的Content-Type为Excel文件类型。
接下来,创建一个文件名`fileName`,该文件名由`startTime`、`endTime`和固定的文本组成。使用`URLEncoder.encode()`对文件名进行编码,并使用`.replaceAll("\\+", "%20")`将编码后的空格替换为"%20"。
然后,通过`response.setHeader()`设置响应头部的Content-Disposition,指定文件的下载方式和文件名。其中,`filename*=utf-8''`用于指定编码格式为UTF-8,`+`符号被替换为"%20"。
接着,使用EasyExcel库来生成Excel文件。`.write(response.getOutputStream(), User.class)`指定将Excel文件写入到响应的输出流中,并且指定数据的类型为`User.class`。`.autoCloseStream(Boolean.FALSE)`表示不自动关闭输出流。
然后,使用`.sheet("质量事故数据表")`指定生成的Excel文件中的Sheet名称为"质量事故数据表"。
最后,使用`.doWrite(listAll)`将数据集合`listAll`写入到Excel文件中,并将该Excel文件通过响应流发送给客户端进行下载。
写一个针对 @GetMapping("exportstuinfo") public CommonResult<?> exportScore(HttpServletResponse response,String fileName) throws IOException { List<StuInfo> stuInfoList = adminMapper.selectStuInfo(); //设置响应头 fileName = new String(fileName.getBytes(),"ISO8859-1"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.setContentType("application/octet-stream;charset=UTF-8"); response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); EasyExcel.write(response.getOutputStream(), StuInfo.class).sheet("列表").doWrite(stuInfoList); return CommonResult.success("导出成功!"); }的apifox接口
您可以使用以下代码作为您的API接口:
```
/**
* 导出学生信息
*
* @param fileName 导出文件名
* @return CommonResult
*/
@GetMapping("exportstuinfo")
public CommonResult<?> exportStuInfo(String fileName, HttpServletResponse response) throws IOException {
List<StuInfo> stuInfoList = adminMapper.selectStuInfo();
//设置响应头
fileName = new String(fileName.getBytes(), "ISO8859-1");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("application/octet-stream;charset=UTF-8");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
EasyExcel.write(response.getOutputStream(), StuInfo.class).sheet("列表").doWrite(stuInfoList);
return CommonResult.success("导出成功!");
}
```
您可以将其复制并粘贴到您的项目中,然后在Apifox中创建一个GET请求,请求路径为您的API接口路径,例如:http://localhost:8080/exportstuinfo。然后在请求参数中添加fileName,值为您想要导出的文件名。最后,您可以在Apifox中测试您的API,以确保它正常工作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)