java的XSSFWorkbook导出简单示例
Posted pz_ww
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java的XSSFWorkbook导出简单示例相关的知识,希望对你有一定的参考价值。
java代码
@GetMapping("/exportExcel") public void exportExcel(@RequestParam(value = "ztId") int ztId, HttpServletResponse response) throws IOException { XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); XSSFSheet sheet = xssfWorkbook.createSheet("测试"); XSSFRow row0 = sheet.createRow(0); row0.createCell(0).setCellValue("编码"); row0.createCell(1).setCellValue("名称"); List<Ce> ces = ceRepository.findAllByZtId(ztId); int num=0; for(var it : ces){ num++; XSSFRow row = sheet.createRow(num); row.createCell(0).setCellValue(it.getCode()); row.createCell(1).setCellValue(it.getName()); } response.setHeader("Content-Disposition", "attachment; filename=Cs.xlsx"); // 响应类型,编码 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 将指定的字节写入此输出流 xssfWorkbook.write(response.getOutputStream()); response.flushBuffer(); xssfWorkbook.close(); }
前端代码:
<el-button type="warning" size="small" @click="exportExcel">导出</el-button> ......... methods: { exportExcel() { httpDownload(`/exportExcel/?ztId=${this.ztId}`, "Ajgl.xlsx"); } } .....
以上是关于java的XSSFWorkbook导出简单示例的主要内容,如果未能解决你的问题,请参考以下文章
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载