@SuppressWarnings("unchecked") private void createBodyRow(Row currentRow, Object[] bodyRow, int[] types) { Cell currentCell; // write the section attributes to the row for (int index = 0; index < bodyRow.length; index++) { // create the cell currentCell = currentRow.createCell(index); // set the format of the cells currentCell.setCellStyle(bodyStyle); if (types[index] != Integer.MAX_VALUE) { currentCell.setCellType(types[index]); } // put the value into the cell switch (types[index]) { case Cell.CELL_TYPE_NUMERIC: currentCell.setCellValue((double) bodyRow[index]); break; case Cell.CELL_TYPE_FORMULA: currentCell.setCellFormula(String.valueOf(bodyRow[index])); break; case Integer.MAX_VALUE: HashMap<String, String> cellInfo = (HashMap<String, String>) bodyRow[index]; // sets the hyperlink to be blue and underline HSSFCellStyle hyperlinkStyle = (HSSFCellStyle) GetBodyCellStyle(workbook); Font hyperlinkFont = workbook.createFont(); hyperlinkFont.setUnderline(Font.U_SINGLE); hyperlinkFont.setColor(HSSFColor.BLUE.index); hyperlinkStyle.setFont(hyperlinkFont); currentCell.setCellStyle(hyperlinkStyle); currentCell.setCellValue(cellInfo.get("Label")); currentCell.setHyperlink(LinkDataToMap(cellInfo)); break; // MAX_VALUE integer will be used to denote hyperlinks case Cell.CELL_TYPE_STRING: currentCell.setCellValue(String.valueOf(bodyRow[index])); break; default: throw new UnsupportedOperationException( "The type " + types[index] + " is not supported." + " Only types 0, 1, 2, and Integer.MAX_VALUE (for hyperlinks)" + " are supported. See the javadoc for org.apache.poi.ss.usermodel.Cell " + "for more information on the types of cells."); } } }
private Font cloneFont(CellStyle cellstyle) { Font newFont = spreadsheet.getWorkbook().createFont(); Font originalFont = spreadsheet.getWorkbook().getFontAt(cellstyle.getFontIndex()); if (originalFont != null) { newFont.setBold(originalFont.getBold()); newFont.setItalic(originalFont.getItalic()); newFont.setFontHeight(originalFont.getFontHeight()); newFont.setUnderline(originalFont.getUnderline()); newFont.setStrikeout(originalFont.getStrikeout()); // This cast an only be done when using .xlsx files XSSFFont originalXFont = (XSSFFont) originalFont; XSSFFont newXFont = (XSSFFont) newFont; newXFont.setColor(originalXFont.getXSSFColor()); } return newFont; }