目录
- 1.接口层
- 2.业务层
- 3.前端请求
- 4.或者直接用tomcat方式导出
1.接口层
@PostMapping("/exprotExcelTemplate")
public void exprotExcelTemplate(HttpServletResponse response) throws Exception {
exportExcelService.exprotExcelTemplate(response);
}
2.业务层
@Override
public void exprotExcelTemplate(HttpServletResponse response) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName ="";
fileName = new String(("xx模板").getBytes(), "ISO8859_1");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()) + ".xlsx");
// 创建 ExcelWriter 对象
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
// 创建 WriteSheet 对象,并指定 sheet 名称
WriteSheet writeSheet = EasyExcel.writerSheet("xx模板").sheetNo(5).build();
// 设置导出的列头(模板的第一行)
List<String> header = Arrays.asList("列名1", "列名2", "列名3"); // 根据实际情况设置列名
excelWriter.write(Collections.singletonList(header), writeSheet);
// 完成导出操作
excelWriter.finish();
}
3.前端请求
axios({
method: "POST",
url: url,
baseURL: baseURL,
data: data,
responseType: "blob", //定义为blob
}).then((data) => {);
4.或者直接用tomcat方式导出
导出excle