this.handleCell(workbook, r, cell, cellNum++);
}
}
private void handleCell(final XSSFWorkbook workbook, final XSSFRow row, final Cell cell, final int cellNum) {
XSSFCell c = row.createCell(cellNum);
XSSFFont font = workbook.createFont();
font.setColor(this.getColor(cell.getFontColor()));
font.setFontName(cell.getFont());
font.setFontHeightInPoints((short) cell.getFontSize());
if (cell.isBold()) {
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
}
if (cell.isItalic()) {
font.setItalic(true);
}
if (cell.isStrikeout()) {
font.setStrikeout(true);
}
if (cell.isUnderline()) {
font.setUnderline(Font.U_SINGLE);
}
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(this.getAlignment(cell.getAlignment()));
style.setVerticalAlignment(this.getVerticalAlignment(cell.getVerticalAlignment()));
style.setWrapText(cell.isWrap());
style.setFont(font);
if (cell.getLeftBorder() != null) {
style.setBorderLeft((short) cell.getLeftBorder().getWidth());
style.setLeftBorderColor(this.getColor(cell.getLeftBorder().getColor()));
}
if (cell.getTopBorder() != null) {
style.setBorderTop((short) cell.getTopBorder().getWidth());
style.setTopBorderColor(this.getColor(cell.getTopBorder().getColor()));
}
if (cell.getRightBorder() != null) {
style.setBorderRight((short) cell.getRightBorder().getWidth());
style.setRightBorderColor(this.getColor(cell.getRightBorder().getColor()));
}
if (cell.getBottomBorder() != null) {
style.setBorderBottom((short) cell.getBottomBorder().getWidth());
style.setBottomBorderColor(this.getColor(cell.getBottomBorder().getColor()));
}
if (cell.getBackgroundColor() != null) {
style.setFillForegroundColor(this.getBackgroundColor(cell.getBackgroundColor()));
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
switch (cell.getType()) {
case BLANK:
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK);
c.setCellValue("");
break;
case BOOLEAN:
if (cell.getValue() != null) {
Object value = cell.getValue();
Boolean bool = null;
if (value instanceof Boolean) {
bool = (Boolean) cell.getValue();
} else {
bool = Boolean.valueOf(value.toString());
}
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN);
c.setCellValue(bool.booleanValue());
} else {
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN);
c.setCellValue("");
}
break;
case ERROR:
if (cell.getValue() != null) {
Object value = cell.getValue();
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR);
c.setCellValue(value.toString());
} else {
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN);
c.setCellValue("");
}
break;
case FORMULA:
if (cell.getValue() != null) {
String formula = cell.getValue().toString();
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA);
c.setCellValue(formula);
} else {
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA);
c.setCellValue("");
}
break;
case NUMERIC:
if (cell.getValue() != null) {
Object value = cell.getValue();
Number number = null;
if (value instanceof Number) {
number = (Number) cell.getValue();
} else {
number = new BigDecimal(value.toString());
}
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC);
c.setCellValue(number.doubleValue());
} else {
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC);
c.setCellValue("");
}
break;
case TEXT:
if (cell.getValue() != null) {
XSSFRichTextString value = new XSSFRichTextString(cell.getValue().toString());
c.setCellValue(value);
} else {
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
c.setCellValue("");
}
break;
default:
c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
c.setCellValue("");
break;
}
c.setCellStyle(style);
}