生成Excle模板,SXSSFWorkbook-2007之後版本不上傳服務器
阿新 • • 發佈:2018-12-28
pre -s down utf-8 throws 生成 odi resp excel
@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
生成Excle模板,SXSSFWorkbook-2007之後版本不上傳服務器