esayExcel
一款导入导出excel特别方便,简单的插件。参考链接:https://github.com/alibaba/easyexcel
// 1前台element-upload组件导入
// 2后台easyExcel导入接口
public Result importDemoExcel(@RequestBody MultipartFile file) {
EasyExcel.read(file.getInputStream(), Demo.class, new
DemoListener(demoDAO)).sheet().doRead();
return "success";
}
// DemoListener为easyExcel,的监听器,为核心的处理类,用来读excel数据,以及校验等,需要实现easyExcel提供的监听器
// demoDAO,对应的业务Dao层。
// 1导出excel,前台模拟表单提交
// 2后台导出接口,官方示例
@GetMapping("download")
public void download(HttpServletResponse response) throws IOException {
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
}
浙公网安备 33010602011771号