/** * 初始化函数 * * @param title 表格标题,传“空值”,表示无标题 * @param headerList 表头列表 */ private void initialize(String title, List<String> headerList) { this.wb = new SXSSFWorkbook(500); this.sheet = wb.createSheet("Export"); this.styles = createStyles(wb); // Create title if (StringUtils.isNotBlank(title)) { Row titleRow = sheet.createRow(rownum++); titleRow.setHeightInPoints(30); Cell titleCell = titleRow.createCell(0); titleCell.setCellStyle(styles.get("title")); titleCell.setCellValue(title); sheet.addMergedRegion( new CellRangeAddress( titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), headerList.size() - 1)); } // Create header if (headerList == null) { throw new RuntimeException("headerList not null!"); } Row headerRow = sheet.createRow(rownum++); headerRow.setHeightInPoints(16); for (int i = 0; i < headerList.size(); i++) { Cell cell = headerRow.createCell(i); cell.setCellStyle(styles.get("header")); String[] ss = StringUtils.split(headerList.get(i), "**", 2); if (ss.length == 2) { cell.setCellValue(ss[0]); Comment comment = this.sheet .createDrawingPatriarch() .createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6)); comment.setString(new XSSFRichTextString(ss[1])); cell.setCellComment(comment); } else { cell.setCellValue(headerList.get(i)); } sheet.autoSizeColumn(i); } for (int i = 0; i < headerList.size(); i++) { int colWidth = sheet.getColumnWidth(i) * 2; sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth); } log.debug("Initialize success."); }
/** * 批量导出 * * @param data * @throws FileNotFoundException */ public void transform(Map<String, Object> data) throws FileNotFoundException { FileInputStream templateIs = new FileInputStream(template); try { XSSFWorkbook bookTmp = (XSSFWorkbook) transformer.transformXLS(templateIs, data); bookTmp.setSheetName(0, "第" + count + "页"); if (count == 1) { book = new SXSSFWorkbook(bookTmp); } else { Sheet newSheet = book.createSheet(bookTmp.getSheetName(0)); Sheet sheet = bookTmp.getSheetAt(0); Util.copySheets(newSheet, sheet); } count++; } catch (ParsePropertyException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } finally { try { templateIs.close(); } catch (IOException e) { e.printStackTrace(); } } }
private Row createRowFrom(IRecordable o) { Row lastRow, r; String[] data; if (dicoLocations.containsKey(o.getClassement())) lastRow = dicoLocations.get(o.getClassement()); else { Sheet logSheet = wb.createSheet(o.getClassement()); lastRow = logSheet.createRow(0); data = o.getTitles(); fillRowWith(lastRow, data); dicoLocations.put(o.getClassement(), lastRow); } r = lastRow.getSheet().createRow(lastRow.getRowNum() + 1); data = o.getRecords(); fillRowWith(r, data); dicoLocations.replace(o.getClassement(), r); return r; }