1、功能描述:读取服务器指定路径下面的模板excle文件然后复制形成一个新的excle(可以修改生成的excle文件的名称),写入内容相应的内容个样式,然后进行下载
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.*; public void exportSalesExcle(HttpServletRequest request, HttpServletResponse response, Map<String, Object> queryDateMap) { String basePath = request.getSession().getServletContext().getRealPath("/" + "resources/template/" + EXCLE_FILE_NAME + ".xlsx"); // 创建模板输入流 FileInputStream fileIn = null; // 创建模板输出流 OutputStream fileOut = null; try { fileIn = new FileInputStream(new File(basePath)); fileOut = response.getOutputStream(); if (fileOut == null || fileIn == null) { return; } //1. 根据指定的文件输入流导入Excel从而产生Workbook对象 Workbook xssfWorkbook = new XSSFWorkbook(fileIn); int sheetCount = 0; sheetCount = xssfWorkbook.getNumberOfSheets(); //移除新生成的excle文件中包含多个sheet页(因为模板中文件中包含多个sheet页,但是自己只需要根据其中的一个sheet页生成一个excle文件进行下载) for (int i = sheetCount; i >= 1; i--) { String sheetName = xssfWorkbook.getSheetAt(i - 1).getSheetName(); if (sheetName != null && (!readSheetName.equals(sheetName))) { xssfWorkbook.removeSheetAt(xssfWorkbook.getSheetIndex(sheetName)); } } //2. 获取Excel文档中的第一个表单 // 默认取第一个子表 XSSFSheet readSheet = (XSSFSheet) xssfWorkbook.getSheet(readSheetName); //3.1创建写的sheet页 XSSFSheet writeXssfSheet = (XSSFSheet) xssfWorkbook.createSheet("Temp"); //3.2创建写行 XSSFRow writeXssfRow; //3.3创建写列,即单元格Cell XSSFCell writeXssfCell; //4.循环取模板样式每行的内容、样式、并写入创建的文档 for (int rowIndex = 0; rowIndex < readSheet.getPhysicalNumberOfRows(); rowIndex++) { XSSFRow readXssfRow = readSheet.getRow(rowIndex); //从第一行开始写入 writeXssfRow = writeXssfSheet.createRow(rowIndex); if (readXssfRow == null) { continue; } //复制值模板内容 for (int cellIndex = 0; cellIndex < readXssfRow.getPhysicalNumberOfCells(); cellIndex++) { //循环单元格数据、样式、背景色 XSSFCell readXssfCell = readXssfRow.getCell(cellIndex); XSSFCellStyle readCellStyle = readXssfCell.getCellStyle(); XSSFColor color = readCellStyle.getFillForegroundXSSFColor(); //按照样本文件格式进行文件头、内容赋值 XSSFCell wirteXssfCell = writeXssfRow.createCell(cellIndex); readCellStyle.setFillBackgroundColor(color); wirteXssfCell.setCellStyle(readCellStyle); wirteXssfCell.setCellValue(ExcleUtils.getString(readXssfCell)); } } //5.将查询的内容写入xlsx文件 xssfWorkbook.removeSheetAt(xssfWorkbook.getSheetIndex("Temp")); ArrayList<ArrayList<SalesVo>> salesInfoList = (ArrayList<ArrayList<SalesVo>>) queryDateMap.get("SalesListInfo"); for (int i = 0; CollectionUtils.isNotEmpty(salesInfoList) && i < salesInfoList.size(); i++) { //一个管理中心下的list集合 ArrayList<SalesVo> middleList = salesInfoList.get(i); //循环取每行的数据 for (int rowIndex = 0; rowIndex < middleList.size(); rowIndex++) { SalesVo SalesInfo = middleList.get(rowIndex); // regionSalesConvert(SalesInfo); int rowNum = readSheet.getLastRowNum() + 1; writeXssfRow = readSheet.createRow(rowNum); for (int cellIndex = 0; cellIndex < readSheet.getRow(0).getPhysicalNumberOfCells(); cellIndex++) { //按照样本文件格式进行文件头、内容赋值 XSSFCell wirteXssfCell = writeXssfRow.createCell(cellIndex); if (cellIndex == 0) { wirteXssfCell.setCellValue(SalesInfo.getArea()); } if (cellIndex == 1) { wirteXssfCell.setCellValue(SalesInfo.getRegionLevel()); } if (cellIndex == 2) { wirteXssfCell.setCellValue(SalesInfo.getRegion()); } if (cellIndex == 3) { wirteXssfCell.setCellValue(SalesInfo.getCompany()); } if (cellIndex == 4) { wirteXssfCell.setCellValue(SalesInfo.getBqxshj()); } if (cellIndex == 5) { wirteXssfCell.setCellValue(SalesInfo.getBqxshj30()); } if (cellIndex == 6) { wirteXssfCell.setCellValue(SalesInfo.getBqxshj50()); } if (cellIndex == 7) { wirteXssfCell.setCellValue(SalesInfo.getTqxshj()); } if (cellIndex == 8) { wirteXssfCell.setCellValue(SalesInfo.getTqxshj30()); } if (cellIndex == 9) { wirteXssfCell.setCellValue(SalesInfo.getTqxshj50()); } if (cellIndex == 10) { wirteXssfCell.setCellValue(SalesInfo.getGrowthRate()); } wirteXssfCell.setCellStyle(ExcleUtils.makeAlignCenter(xssfWorkbook)); //区域所在的中心合计:合并0、1、2、3列 if(SalesInfo.getAreaAlignCenterFlag()){ wirteXssfCell.setCellStyle(ExcleUtils.makeGroundColor(xssfWorkbook,255,233,198)); if(cellIndex==3){ CellRangeAddress areaAlignCenter = new CellRangeAddress(rowNum, rowNum, 0, 3); // 参数都是从O开始 readSheet.addMergedRegion(areaAlignCenter); } } //区域合计:合并1、2、3列 if(SalesInfo.getRegionAlignCenterFlag()){ wirteXssfCell.setCellStyle(ExcleUtils.makeGroundColor(xssfWorkbook,203,227,254)); if(cellIndex==3){ CellRangeAddress regionAlignCenter = new CellRangeAddress(rowNum, rowNum, 1, 3); // 参数都是从O开始 readSheet.addMergedRegion(regionAlignCenter); } if (cellIndex == 1) { wirteXssfCell.setCellValue(SalesInfo.getRegion()); } } } } } String userAgent = request.getHeader("USER-AGENT"); String fileName = ExcleUtils.dealChineseFileName(userAgent, FILE_NAME); Date now=new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd"); String exportDate=dateFormat.format(now); response.setHeader("Content-disposition", "attachment; filename=" + fileName + exportDate +".xlsx"); response.setContentType("application/octet-stream;charset=utf-8"); xssfWorkbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (Exception e) { LOGGER.info("边读边写生成xlsx时异常:{}", EXCLE_FILE_NAME, e); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { LOGGER.error("关闭输出流异常:{}", e); } } if (fileIn != null) { try { fileIn.close(); } catch (IOException e) { LOGGER.error("关闭输入流异常:{}", e); } } } }
/** * [功能描述]:设置单元格的背景色:存在区域合计 */ public static XSSFCellStyle makeGroundColor(Workbook wb, int r, int g, int b) { XSSFCellStyle groundColorStyle = (XSSFCellStyle) wb.createCellStyle(); groundColorStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置填充方案 groundColorStyle.setFillForegroundColor(new XSSFColor(new Color(r, g, b)));//设置填充颜色(可以通过抓取工具获取) groundColorStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//水平居中 groundColorStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//水平居中 groundColorStyle.setBorderBottom(CellStyle.BORDER_THIN); groundColorStyle.setBorderTop(CellStyle.BORDER_THIN); groundColorStyle.setBorderLeft(CellStyle.BORDER_THIN); groundColorStyle.setBorderRight(CellStyle.BORDER_THIN); return groundColorStyle; }
/** * [功能描述]:设置单元格样式:水平居中、水平居中、细边线 */ public static XSSFCellStyle makeAlignCenter(Workbook wb) { XSSFCellStyle alignStyle = (XSSFCellStyle) wb.createCellStyle(); alignStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//水平居中 alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//水平居中 alignStyle.setBorderBottom(CellStyle.BORDER_THIN); alignStyle.setBorderTop(CellStyle.BORDER_THIN); alignStyle.setBorderLeft(CellStyle.BORDER_THIN); alignStyle.setBorderRight(CellStyle.BORDER_THIN); return alignStyle; }
疑问:可以在读取模板生成excle的时候只读取其中的某个sheet页,进行生成,而不是在读取全部内容生成新excle后将不需要的sheet页进行删除吗?