HSSFCellStyle style = cell.getCellStyle(); double nVal = cell.getNumericCellValue(); short df = style.getDataFormat();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
final HSSFCell dataCell = dataRow.createCell(1); dataCell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0")); dataCell.setCellValue(123456);
private static void copyCellStyle(HSSFCell cell, HSSFCellStyle newCellStyle) { newCellStyle.setAlignment(cell.getCellStyle().getAlignment()); newCellStyle.setBorderBottom(cell.getCellStyle().getBorderBottom()); newCellStyle.setBorderLeft(cell.getCellStyle().getBorderLeft()); newCellStyle.setBorderRight(cell.getCellStyle().getBorderRight()); newCellStyle.setBorderTop(cell.getCellStyle().getBorderTop()); newCellStyle.setBottomBorderColor(cell.getCellStyle().getBottomBorderColor()); newCellStyle.setDataFormat(cell.getCellStyle().getDataFormat()); newCellStyle.setFillBackgroundColor(cell.getCellStyle().getFillBackgroundColor()); newCellStyle.setFillForegroundColor(cell.getCellStyle().getFillForegroundColor()); newCellStyle.setFillPattern(cell.getCellStyle().getFillPattern()); newCellStyle.setFont(cell.getCellStyle().getFont(cell.getSheet().getWorkbook())); newCellStyle.setHidden(cell.getCellStyle().getHidden()); newCellStyle.setIndention(cell.getCellStyle().getIndention()); newCellStyle.setLeftBorderColor(cell.getCellStyle().getLeftBorderColor()); newCellStyle.setLocked(cell.getCellStyle().getLocked()); newCellStyle.setRightBorderColor(cell.getCellStyle().getRightBorderColor()); newCellStyle.setRotation(cell.getCellStyle().getRotation()); newCellStyle.setShrinkToFit(cell.getCellStyle().getShrinkToFit()); newCellStyle.setTopBorderColor(cell.getCellStyle().getTopBorderColor()); // newCellStyle.setUserStyleName(cell.getCellStyle().getUserStyleName()); -> ignore newCellStyle.setVerticalAlignment(cell.getCellStyle().getVerticalAlignment()); newCellStyle.setWrapText(cell.getCellStyle().getWrapText()); }
protected BigDecimal getNumericValue(HSSFCell cell) { logger.debug("getNumericValue(cell={}) - start", cell); String formatString = cell.getCellStyle().getDataFormatString(); String resultString = null; double cellValue = cell.getNumericCellValue(); if ((formatString != null)) { if (!formatString.equals("General") && !formatString.equals("@")) { logger.debug("formatString={}", formatString); DecimalFormat nf = new DecimalFormat(formatString, symbols); resultString = nf.format(cellValue); } } BigDecimal result; if (resultString != null) { try { result = new BigDecimal(resultString); } catch (NumberFormatException e) { logger.debug("Exception occurred while trying create a BigDecimal. value={}", resultString); // Probably was not a BigDecimal format retrieved from the // excel. Some // date formats are not yet recognized by HSSF as DateFormats so // that // we could get here. result = toBigDecimal(cellValue); } } else { result = toBigDecimal(cellValue); } return result; }
byte underline = cell.getCellStyle().getFont(workbook).getUnderline(); if (underline == 1) { primaryKeyList.add(columnName);
if (HSSFDateUtil.isValidExcelDate(d)) HSSFCellStyle style = cell.getCellStyle(); int i = style.getDataFormat(); switch (i)
InputStream inp; HSSFWorkbook wb = null; HSSFSheet sheet = null; HSSFRow hssf_DataRow; HSSFCell cell = null; inp = new FileInputStream(FileName); wb = new HSSFWorkbook(inp); sheet = wb.getSheetAt(1); hssf_DataRow = sheet.createRow((short) 1); cell = hssf_DataRow.createCell(1); HSSFFont font = (HSSFFont) sheet.getWorkbook().createFont(); HSSFCellStyle style = (HSSFCellStyle) cell.getCellStyle(); font.setBold(true); style.setFont(font); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFillForegroundColor(new HSSFColor.BLUE().getIndex()); cell.setCellStyle(style);
switch (type) { case HSSFCell.CELL_TYPE_NUMERIC: HSSFCellStyle style = cell.getCellStyle(); if (HSSFDateUtil.isCellDateFormatted(cell)) { return getDateValue(cell);
HSSFCellStyle templateStyle = templateCell.getCellStyle(); HSSFCellStyle resultStyle = copyCellStyle(templateStyle); resultCell.setCellStyle(resultStyle);
HSSFCellStyle templateStyle = templateCell.getCellStyle(); HSSFCellStyle resultStyle = copyCellStyle(templateStyle); resultCell.setCellStyle(resultStyle);
private void fixUpCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFCell upCell = sheet.getRow(rowIndex).getCell(columnIndex); if (upCell != null) { HSSFCellStyle upCellStyle = upCell.getCellStyle(); if (upCellStyle.getBorderBottom() != cellStyle.getBorderTop() || upCellStyle.getBottomBorderColor() != cellStyle.getTopBorderColor()) { HSSFCellStyle draftUpStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(upCellStyle, draftUpStyle); draftUpStyle.setBorderBottom(cellStyle.getBorderTop()); draftUpStyle.setBottomBorderColor(cellStyle.getTopBorderColor()); HSSFCellStyle newUpStyle = styleCache.getCellStyleByTemplate(draftUpStyle); if (newUpStyle == null) { newUpStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftUpStyle); styleCache.processCellStyle(newUpStyle); } upCell.setCellStyle(newUpStyle); } } }
private void fixLeftCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFCell leftCell = sheet.getRow(rowIndex).getCell(columnIndex); if (leftCell != null) { HSSFCellStyle leftCellStyle = leftCell.getCellStyle(); if (leftCellStyle.getBorderRightEnum() != cellStyle.getBorderLeftEnum() || leftCellStyle.getRightBorderColor() != cellStyle.getLeftBorderColor()) { HSSFCellStyle draftLeftStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(leftCellStyle, draftLeftStyle); draftLeftStyle.setBorderRight(cellStyle.getBorderLeftEnum()); draftLeftStyle.setRightBorderColor(cellStyle.getLeftBorderColor()); HSSFCellStyle newLeftStyle = styleCache.getCellStyleByTemplate(draftLeftStyle); if (newLeftStyle == null) { newLeftStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftLeftStyle); styleCache.processCellStyle(newLeftStyle); } leftCell.setCellStyle(newLeftStyle); } } }
private void fixUpCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFCell upCell = sheet.getRow(rowIndex).getCell(columnIndex); if (upCell != null) { HSSFCellStyle upCellStyle = upCell.getCellStyle(); if (upCellStyle.getBorderBottomEnum() != cellStyle.getBorderTopEnum() || upCellStyle.getBottomBorderColor() != cellStyle.getTopBorderColor()) { HSSFCellStyle draftUpStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(upCellStyle, draftUpStyle); draftUpStyle.setBorderBottom(cellStyle.getBorderTopEnum()); draftUpStyle.setBottomBorderColor(cellStyle.getTopBorderColor()); HSSFCellStyle newUpStyle = styleCache.getCellStyleByTemplate(draftUpStyle); if (newUpStyle == null) { newUpStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftUpStyle); styleCache.processCellStyle(newUpStyle); } upCell.setCellStyle(newUpStyle); } } }
private void fixLeftCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFCell leftCell = sheet.getRow(rowIndex).getCell(columnIndex); if (leftCell != null) { HSSFCellStyle leftCellStyle = leftCell.getCellStyle(); if (leftCellStyle.getBorderRight() != cellStyle.getBorderLeft() || leftCellStyle.getRightBorderColor() != cellStyle.getLeftBorderColor()) { HSSFCellStyle draftLeftStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(leftCellStyle, draftLeftStyle); draftLeftStyle.setBorderRight(cellStyle.getBorderLeft()); draftLeftStyle.setRightBorderColor(cellStyle.getLeftBorderColor()); HSSFCellStyle newLeftStyle = styleCache.getCellStyleByTemplate(draftLeftStyle); if (newLeftStyle == null) { newLeftStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftLeftStyle); styleCache.processCellStyle(newLeftStyle); } leftCell.setCellStyle(newLeftStyle); } } }
private void fixRightCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFCell rightCell = sheet.getRow(rowIndex).getCell(columnIndex); if (rightCell != null) { HSSFCellStyle rightCellStyle = rightCell.getCellStyle(); if (rightCellStyle.getBorderLeft() != cellStyle.getBorderRight() || rightCellStyle.getLeftBorderColor() != cellStyle.getRightBorderColor()) { HSSFCellStyle draftRightStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(rightCellStyle, draftRightStyle); draftRightStyle.setBorderLeft(cellStyle.getBorderRight()); draftRightStyle.setLeftBorderColor(cellStyle.getRightBorderColor()); HSSFCellStyle newRightStyle = styleCache.getCellStyleByTemplate(draftRightStyle); if (newRightStyle == null) { newRightStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftRightStyle); styleCache.processCellStyle(newRightStyle); } rightCell.setCellStyle(newRightStyle); } } }
private void fixRightCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFCell rightCell = sheet.getRow(rowIndex).getCell(columnIndex); if (rightCell != null) { HSSFCellStyle rightCellStyle = rightCell.getCellStyle(); if (rightCellStyle.getBorderLeftEnum() != cellStyle.getBorderRightEnum() || rightCellStyle.getLeftBorderColor() != cellStyle.getRightBorderColor()) { HSSFCellStyle draftRightStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(rightCellStyle, draftRightStyle); draftRightStyle.setBorderLeft(cellStyle.getBorderRightEnum()); draftRightStyle.setLeftBorderColor(cellStyle.getRightBorderColor()); HSSFCellStyle newRightStyle = styleCache.getCellStyleByTemplate(draftRightStyle); if (newRightStyle == null) { newRightStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftRightStyle); styleCache.processCellStyle(newRightStyle); } rightCell.setCellStyle(newRightStyle); } } }
private void fixDownCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFRow nextRow = sheet.getRow(rowIndex); if (nextRow != null) { HSSFCell downCell = nextRow.getCell(columnIndex); if (downCell != null) { HSSFCellStyle downCellStyle = downCell.getCellStyle(); if (downCellStyle.getBorderTop() != cellStyle.getBorderBottom() || downCellStyle.getTopBorderColor() != cellStyle.getBottomBorderColor()) { HSSFCellStyle draftDownStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(downCellStyle, draftDownStyle); draftDownStyle.setBorderTop(cellStyle.getBorderBottom()); draftDownStyle.setTopBorderColor(cellStyle.getBottomBorderColor()); HSSFCellStyle newDownStyle = styleCache.getCellStyleByTemplate(draftDownStyle); if (newDownStyle == null) { newDownStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftDownStyle); styleCache.processCellStyle(newDownStyle); } downCell.setCellStyle(newDownStyle); } } } } }
private void fixDownCell(HSSFSheet sheet, int rowIndex, int columnIndex, HSSFCellStyle cellStyle) { HSSFRow nextRow = sheet.getRow(rowIndex); if (nextRow != null) { HSSFCell downCell = nextRow.getCell(columnIndex); if (downCell != null) { HSSFCellStyle downCellStyle = downCell.getCellStyle(); if (downCellStyle.getBorderTopEnum() != cellStyle.getBorderBottomEnum() || downCellStyle.getTopBorderColor() != cellStyle.getBottomBorderColor()) { HSSFCellStyle draftDownStyle = HSSFWorkbookHelper.createDetachedCellStyle(sheet.getWorkbook()); XslStyleHelper.cloneStyleRelations(downCellStyle, draftDownStyle); draftDownStyle.setBorderTop(cellStyle.getBorderBottomEnum()); draftDownStyle.setTopBorderColor(cellStyle.getBottomBorderColor()); HSSFCellStyle newDownStyle = styleCache.getCellStyleByTemplate(draftDownStyle); if (newDownStyle == null) { newDownStyle = HSSFWorkbookHelper.adoptDetachedCellStyle(sheet.getWorkbook(), draftDownStyle); styleCache.processCellStyle(newDownStyle); } downCell.setCellStyle(newDownStyle); } } } } }
@Override protected void writeTableBodyCloser(TableModel model) throws Exception { // write totals, if there are any boolean hasTotals = false; for (HeaderCell cell : model.getHeaderCellList()) { hasTotals = hasTotals || cell.isTotaled(); } if (!hasTotals) { return; } TableTotaler tt = model.getTotaler(); writeRowOpener(null); for (HeaderCell cell : model.getHeaderCellList()) { writeColumnOpener(null); Object columnValue = (cell.isTotaled()) ? tt.getTotalForColumn(cell.getColumnNumber(), 0) : null; writeCellValue(columnValue); CellStyle st = this.utils.getNewCellStyle(); st.cloneStyleFrom(this.currentCell.getCellStyle()); st.setBorderTop(BorderStyle.THIN); st.setTopBorderColor(IndexedColors.BLACK.getIndex()); this.currentCell.setCellStyle(st); writeColumnCloser(null); } writeRowCloser(null); }