easyexcel版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<artifactId>poi</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
POST方式
后台:
@RequestMapping("/excel")
public class TestController {
@PostMapping(value = "/export")
public void export(HttpServletResponse response) throws Exception {
List<User> userList = getUserList();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
response.setCharacterEncoding("utf-8");
try{
EasyExcel.write(response.getOutputStream(), BaseProductClassDto.class)
.sheet("sheet1")
.doWrite(baseProductClassList);
}catch (Exception e) {
e.printStackTrace();
}
}
private List<User> getUserList(){
List<User> userList = new ArrayList<>();
User user = new User();
user.setUserName("张三");
user.setSex("男");
user.setAge("10");
userList.add(user);
return userList;
}
}
前端vue:
<sd-button-group>
<a-button
type="primary"
v-action:export
@click="exportData"
>数据导出</a-button>
</sd-button-group>
exportData() {
// const headers = { 'Content-Type': 'application/octet-stream' }
axios.post('/excel/export',{},
{ responseType: 'blob' }).then((res) => {
const link = document.createElement('a')
const blob = new Blob([res], { type: 'application/vnd.ms-excel'})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = '用户列表.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
}
导出结果:
GET方式
后台:
@RequestMapping("/excel")
public class TestController {
@GetMapping(value = "/export")
public void export(HttpServletResponse response) throws Exception {
List<User> userList = getUserList();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
response.setCharacterEncoding("utf-8");
try{
EasyExcel.write(response.getOutputStream(), BaseProductClassDto.class)
.sheet("sheet1")
.doWrite(baseProductClassList);
}catch (Exception e) {
e.printStackTrace();
}
}
private List<User> getUserList(){
List<User> userList = new ArrayList<>();
User user = new User();
user.setUserName("张三");
user.setSex("男");
user.setAge("10");
userList.add(user);
return userList;
}
}
前端vue:
axios({url: '/excel/export',method: 'get',responseType: 'blob'})
.then((res) => {
const link = document.createElement('a')
const blob = new Blob([res], { type: 'application/vnd.ms-excel'})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = '用户列表.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
导出结果:
参考资料:
vue+axios 与spring boot EasyExcel实现后台导出excel并下载_feng2036的博客-优快云博客