@GetMapping("/downTemplateExcel") public void downTemplateExcel(HttpServletResponse response) throws IOException { SXSSFWorkbook workbook = new SXSSFWorkbook(); SXSSFSheet sheet = workbook.createSheet("sheet"); /** * 具体导出名字再议 * * !!!!!! */ String fileName = "test" + ".xlsx"; int rowNum = 1; String[] headers = { "合作商ID", "合作商名称", "类型", "官网"}; SXSSFRow row = sheet.createRow(0); for(int i=0;i<headers.length;i++){ SXSSFCell cell = row.createCell(i); XSSFRichTextString text = new XSSFRichTextString(headers[i]); cell.setCellValue(text); } SXSSFRow row1 = sheet.createRow(rowNum); row1.createCell(0).setCellValue(""); row1.createCell(1).setCellValue(""); row1.createCell(2).setCellValue(""); row1.createCell(3).setCellValue(""); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.setCharacterEncoding("utf-8"); response.setContentType("application/x-download"); response.flushBuffer(); workbook.write(response.getOutputStream()); }
如题,返回的是个url直接前端href即可。
基本参考的就是下面的例子,只不过把版本从2003升级到了2007excle,谢谢。
https://www.w3xue.com/exp/article/201812/11233.html