private static void populateWorkbook(NodeList sheetList, Map cellStyles, HSSFWorkbook workbook) throws ExcelTransformerException { if (LOG.isLoggable(Level.FINE)) { LOG.entering( SimpleExcelRenderer.class.getName(), "populateWorkbook", String.valueOf(sheetList.getLength())); } for (int k = 0; k < sheetList.getLength(); k++) { Element sheet = (Element) sheetList.item(k); NodeList rowList = sheet.getElementsByTagName("row"); if (rowList.getLength() > 0) { HSSFSheet hSheet = workbook.createSheet(sheet.getAttribute("name")); for (int i = 0; i < rowList.getLength(); i++) { Element row = (Element) rowList.item(i); HSSFRow hRow = hSheet.createRow(i); short cellCounter = 0; NodeList cells = row.getChildNodes(); for (short j = 0; j < cells.getLength(); j++) { Node cell = cells.item(j); if (cell.getNodeType() == Node.ELEMENT_NODE && cell.getNodeName().equals("cell")) { Element cellE = (Element) cell; String value = XmlUtils.getElementText(cellE); String style = cellE.getAttribute("style"); String colSpan = cellE.getAttribute("colspan"); int colSpanI = 0; if (Utils.hasContent(colSpan)) { colSpanI = Integer.parseInt(colSpan); hSheet.addMergedRegion( new Region(i, cellCounter, i, (short) (cellCounter + colSpanI - 1))); } HSSFCell hCell = hRow.createCell(cellCounter); if (Utils.hasContent(style)) hCell.setCellStyle((HSSFCellStyle) cellStyles.get(style)); hCell.setCellValue(value); cellCounter++; } } } } } if (LOG.isLoggable(Level.FINE)) { LOG.exiting(SimpleExcelRenderer.class.getName(), "populateWorkbook"); } }
private static Map readFonts(NodeList fontList, HSSFWorkbook workbook) throws ExcelTransformerException { if (LOG.isLoggable(Level.FINE)) { LOG.entering( SimpleExcelRenderer.class.getName(), "readFonts", String.valueOf(fontList.getLength())); } Map results = new HashMap(); for (int i = 0; i < fontList.getLength(); i++) { Element fontNode = (Element) fontList.item(i); String name = fontNode.getAttribute("name"); HSSFFont font = workbook.createFont(); NodeList children = fontNode.getChildNodes(); for (int j = 0; j < children.getLength(); j++) { Node child = children.item(j); if (child.getNodeType() == Node.ELEMENT_NODE) { Element childE = (Element) child; String value = XmlUtils.getElementText(childE); if (childE.getNodeName().equals("family")) { font.setFontName(value); } else { try { Field field = HSSFFont.class.getField(value); if (childE.getNodeName().equals("boldweight")) { font.setBoldweight(field.getShort(null)); } } catch (Throwable th) { throw new ExcelTransformerException( "An error occurred while processing " + value + " on the excel font."); } } } } results.put(name, font); } if (LOG.isLoggable(Level.FINE)) { LOG.exiting(SimpleExcelRenderer.class.getName(), "readFonts"); } return results; }
private static Map readStyles(NodeList styleList, Map fonts, HSSFWorkbook workbook) throws ExcelTransformerException { if (LOG.isLoggable(Level.FINE)) { LOG.entering( SimpleExcelRenderer.class.getName(), "readStyles", String.valueOf(styleList.getLength())); } Map results = new HashMap(); for (int i = 0; i < styleList.getLength(); i++) { Element styleNode = (Element) styleList.item(i); String name = styleNode.getAttribute("name"); HSSFCellStyle style = workbook.createCellStyle(); NodeList children = styleNode.getChildNodes(); for (int j = 0; j < children.getLength(); j++) { Node child = children.item(j); if (child.getNodeType() == Node.ELEMENT_NODE) { Element childE = (Element) child; String value = XmlUtils.getElementText(childE); if (childE.getNodeName().equals("font")) { HSSFFont font = (HSSFFont) fonts.get(value); if (font == null) { throw new ExcelTransformerException("Unable to locate referenced font: " + value); } style.setFont(font); } else if (childE.getNodeName().equals("builtinformat")) { style.setDataFormat(HSSFDataFormat.getBuiltinFormat(value)); } else if (childE.getNodeName().equals("dataformat")) { style.setDataFormat(workbook.createDataFormat().getFormat(value)); } } } results.put(name, style); } if (LOG.isLoggable(Level.FINE)) { LOG.exiting(SimpleExcelRenderer.class.getName(), "readStyles"); } return results; }