ruoyi导出设置excel文件名称
时间: 2023-06-12 08:05:17 浏览: 790
若依(ruoyi)导出的nacos配置文件
在Ruoyi中导出Excel文件时,可以通过设置response的header来指定文件名称。具体操作步骤如下:
1. 在Controller中获取导出数据,并将数据转换为Excel文件格式,例如:
```java
List<User> userList = userService.selectUserList(user);
ExcelUtil<User> util = new ExcelUtil<User>(User.class);
Workbook workbook = util.exportExcel(userList, "用户数据");
```
2. 设置response的header,指定文件名称,例如:
```java
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户数据.xlsx", "utf-8"));
```
完整代码示例:
```java
@GetMapping("/export")
public void export(User user, HttpServletResponse response) throws IOException
{
List<User> userList = userService.selectUserList(user);
ExcelUtil<User> util = new ExcelUtil<User>(User.class);
Workbook workbook = util.exportExcel(userList, "用户数据");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户数据.xlsx", "utf-8"));
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
```
上述代码中,文件名称为"用户数据.xlsx",可以根据需要自行修改。
阅读全文