poi多sheet,模板导出数据

编程入门 行业动态 更新时间:2024-10-27 02:29:16

poi多sheet,<a href=https://www.elefans.com/category/jswz/34/1770549.html style=模板导出数据"/>

poi多sheet,模板导出数据

/*** 导出Excel* @param response 响应对象* @param headName 表头* @param  List     数据*/public static void exportExcel(HttpServletResponse response, String headName, List<数据对象> list) throws IOException {//读取模板Resource resource=new ClassPathResource("file/模板.xlsx");Workbook workbook=new XSSFWorkbook(resource.getInputStream());//设置数据剧中CellStyle cellStyle = workbook.createCellStyle();cellStyle.setAlignment(HorizontalAlignment.CENTER);cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//获得 sheetSheet sheet= workbook.getSheetAt(0);//获得第一行 设置表头内容Row headRow = sheet.getRow(0);Cell headRowCell = headRow.getCell(0);headRowCell.setCellValue(headName);//从第表格第三行开始写入数据,根据模板设置index开始的值int index=2;Cell cell=null;for (XXX xxx : list) {//创建行Row rowData = sheet.createRow(index++);/创建cell 写入值cell=rowData.createCell(0);cell.setCellStyle(cellStyle);cell.setCellValue(xxx.getXXX());cell=rowData.createCell(1);cell.setCellStyle(cellStyle);cell.setCellValue(xxx.getXXX());...}//如果是多数据源 导出 多个sheet  //获得 第二个 sheetSheet sheet2= workbook.getSheetAt(1);//此处写法不一样String fileName = URLEncoder.encode("表格名称", "utf-8");response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");workbook.write(response.getOutputStream());//String fileName = URLEncoder.encode("表格名称.xlsx", "utf-8");//response.setHeader("content-type", "application/octet-stream;charset=utf-8");//response.setHeader("content-disposition", "attachment; ");//response.setHeader("filename",fileName);//workbook.write(response.getOutputStream());}

更多推荐

poi多sheet,模板导出数据

本文发布于:2023-11-15 23:20:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   数据   poi   sheet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!