Java Code Examples for org.apache.poi.ss.usermodel.Cell#getCellStyle()
The following examples show how to use
org.apache.poi.ss.usermodel.Cell#getCellStyle() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExcelUtils.java From onetwo with Apache License 2.0 | 6 votes |
public static void copyCellStyle(Cell source, Cell target){ CellStyle style = source.getCellStyle(); if(style!=null){ //TODO:会影响性能, 可缓存。。。 CellStyle newCellStyle = source.getRow().getSheet().getWorkbook().createCellStyle(); newCellStyle.cloneStyleFrom(style); target.setCellStyle(style); } if(source.getCellComment()!=null){ target.setCellComment(source.getCellComment()); } if(source.getHyperlink()!=null){ target.setHyperlink(source.getHyperlink()); } target.setCellType(source.getCellType()); }
Example 2
Source File: ExcelAdjacencyMatrixExtractor.java From wandora with GNU General Public License v3.0 | 6 votes |
public boolean hasValue(Cell cell) { if(ADD_CELL_COLOR_AS_PLAYER || INTERPRET_COLOR_AS_VALID_CELL_VALUE) { CellStyle style = cell.getCellStyle(); short color = style.getFillBackgroundColor(); if(color != 0) { return true; } } String str = getCellValueAsString(cell); if(str == null) return false; if(INTERPRET_FALSE_AS_EMPTY_CELL && "false".equalsIgnoreCase(str)) return false; if(INTERPRET_ZERO_AS_EMPTY_CELL && "0".equalsIgnoreCase(str)) return false; if(INTERPRET_ZERO_LENGTH_STRING_AS_EMPTY_CELL && "".equalsIgnoreCase(str)) return false; return true; }
Example 3
Source File: StylerHelper.java From easypoi with Apache License 2.0 | 6 votes |
public void printStyles(Workbook wb) { if (DEFAULTS_CLASS_CSS == null) { DEFAULTS_CLASS_CSS = getDefaultsClassCss(); } out.format(DEFAULTS_CLASS_CSS); Set<CellStyle> seen = new HashSet<CellStyle>(); sheet = wb.getSheetAt(sheetNum); Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) { Row row = rows.next(); for (Cell cell : row) { CellStyle style = cell.getCellStyle(); if (!seen.contains(style)) { printStyle(style); seen.add(style); } } } }
Example 4
Source File: StylerHelper.java From autopoi with Apache License 2.0 | 6 votes |
public void printStyles(Workbook wb) { if (DEFAULTS_CLASS_CSS == null) { DEFAULTS_CLASS_CSS = getDefaultsClassCss(); } out.format(DEFAULTS_CLASS_CSS); Set<CellStyle> seen = new HashSet<CellStyle>(); sheet = wb.getSheetAt(sheetNum); Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) { Row row = rows.next(); for (Cell cell : row) { CellStyle style = cell.getCellStyle(); if (!seen.contains(style)) { printStyle(style); seen.add(style); } } } }
Example 5
Source File: XsStyle.java From excel-io with MIT License | 5 votes |
@Override public CellStyle attachTo(final Cell cell) { final CellStyle style = cell.getCellStyle(); for (final Props<CellStyle> property : this.properties) { property.accept(style); } return style; }
Example 6
Source File: VerticalRecordsProcessor.java From xlsmapper with Apache License 2.0 | 5 votes |
/** * セルの書式をコピーする。 * <p>コピー先のセルの種類は、空セルとする。</p> * <p>結合情報も列方向の結合をコピーする。</p> * * @since 2.0 * @param fromCell コピー元 * @param toCell コピー先 */ private void copyCellStyle(final Cell fromCell, final Cell toCell) { final CellStyle style = fromCell.getCellStyle(); toCell.setCellStyle(style); toCell.setCellType(CellType.BLANK); // 縦方向に結合されている場合、結合情報のコピーする。(XlsArrayColumns用) final Sheet sheet = fromCell.getSheet(); final CellRangeAddress mergedRegion = POIUtils.getMergedRegion(sheet, fromCell.getRowIndex(), fromCell.getColumnIndex()); final int mergedSize = POIUtils.getRowSize(mergedRegion); if(mergedSize >= 2) { CellRangeAddress newMergedRegion = POIUtils.getMergedRegion(sheet, toCell.getRowIndex(), toCell.getColumnIndex()); if(newMergedRegion != null) { // 既に結合している場合 - 通常はありえない。 return; } newMergedRegion = POIUtils.mergeCells(sheet, toCell.getColumnIndex(), mergedRegion.getFirstRow(), toCell.getColumnIndex(), mergedRegion.getLastRow()); // 結合先のセルの書式も設定する for(int i=1; i < mergedSize; i++) { Cell mergedFromCell = POIUtils.getCell(sheet, fromCell.getColumnIndex(), toCell.getRowIndex()+i); Cell mergedToCell = POIUtils.getCell(sheet, toCell.getColumnIndex(), toCell.getRowIndex()+i); mergedToCell.setCellStyle(mergedFromCell.getCellStyle()); mergedToCell.setCellType(CellType.BLANK); } } }
Example 7
Source File: Borders1ReportTest.java From birt with Eclipse Public License 1.0 | 5 votes |
private void assertBorder( Sheet sheet, int row, int col, short bottom, short left, short right, short top ) { Cell cell = sheet.getRow(row).getCell(col); CellStyle style = cell.getCellStyle(); assertSingleBorder( sheet, row, "bottom", bottom, style.getBorderBottom() ); assertSingleBorder( sheet, row, "left", left, style.getBorderLeft() ); assertSingleBorder( sheet, row, "right", right, style.getBorderRight() ); assertSingleBorder( sheet, row, "top", top, style.getBorderTop() ); }
Example 8
Source File: Borders2ReportTest.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Check that the borders for a given cell match the expected values. * This is complicated by the fact that POI will not always give a particular cell the borders that are seen in Excel * - neighbouring cells may override the values for the chosen cell. * I don't know how to tell which takes precedence, but the following works for the tests I've carried out. */ public static void assertBorder( Sheet sheet, int row, int col, short bottom, short left, short right, short top ) { Row curRow = sheet.getRow( row ); Row prevRow = ( row > 0 ) ? sheet.getRow( row - 1 ) : null; Row nextRow = sheet.getRow( row + 1 ); Cell cell = curRow.getCell(col); CellStyle style = cell.getCellStyle(); Cell cellUp = ( prevRow == null ) ? null : prevRow.getCell( col ); Cell cellDown = ( nextRow == null ) ? null : nextRow.getCell( col ); Cell cellLeft = ( col == 0 ) ? null : curRow.getCell( col - 1 ); Cell cellRight = curRow.getCell( col + 1 ); CellStyle styleUp = ( cellUp == null ) ? null : cellUp.getCellStyle(); CellStyle styleDown = ( cellDown == null ) ? null : cellDown.getCellStyle(); CellStyle styleLeft = ( cellLeft == null ) ? null : cellLeft.getCellStyle(); CellStyle styleRight = ( cellRight == null ) ? null : cellRight.getCellStyle(); System.out.println( "style == " + style ); System.out.println( "style == " + style ); if( ( top != style.getBorderTop() ) && ( ( styleUp == null ) || ( top != styleUp.getBorderBottom() ) ) ) { assertEquals( top, style.getBorderTop() ); } if( ( bottom != style.getBorderBottom() ) && ( ( styleDown == null ) || ( bottom != styleDown.getBorderTop() ) ) ) { assertEquals( bottom, style.getBorderBottom() ); } if( ( left != style.getBorderLeft() ) && ( ( styleLeft == null ) || ( left != styleLeft.getBorderRight() ) ) ) { assertEquals( left, style.getBorderLeft() ); } if( ( right != style.getBorderRight() ) && ( ( styleRight == null ) || ( right != styleRight.getBorderLeft() ) ) ) { assertEquals( right, style.getBorderRight() ); } }
Example 9
Source File: POIUtils.java From xlsmapper with Apache License 2.0 | 5 votes |
/** * 結合を考慮してセルの罫線(右部)を取得する。 * * @param cell セル * @return {@literal BorderStyle} * @throws IllegalArgumentException {@literal cell is null.} */ public static BorderStyle getBorderLeft(final Cell cell) { ArgUtils.notNull(cell, "cell"); final Sheet sheet = cell.getSheet(); CellRangeAddress mergedRegion = getMergedRegion(sheet, cell.getRowIndex(), cell.getColumnIndex()); final Cell target; if(mergedRegion == null) { // 結合されていない場合 target = cell; } else { if(mergedRegion.getFirstColumn() == cell.getColumnIndex()) { // 引数のCellが左部のセルの場合 target = cell; } else { target = getCell(sheet, mergedRegion.getFirstColumn(), cell.getRowIndex()); } } final CellStyle style = target.getCellStyle(); if(style == null) { return BorderStyle.NONE; } else { return style.getBorderLeftEnum(); } }
Example 10
Source File: FontsReportTest.java From birt with Eclipse Public License 1.0 | 5 votes |
private void assertFontCell( Sheet sheet, int row, int col, String contents, String fontName, int fontHeight ) { Cell cell = sheet.getRow(row).getCell(col); CellStyle style = cell.getCellStyle(); assertEquals( contents, cell.getStringCellValue()); assertEquals( fontName, sheet.getWorkbook().getFontAt( style.getFontIndex() ).getFontName().replace("\"", "") ); assertEquals( fontHeight, sheet.getWorkbook().getFontAt( style.getFontIndex() ).getFontHeightInPoints() ); }
Example 11
Source File: StyleManagerUtils.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Check whether a cell is empty and unformatted. * @param cell * The cell to consider. * @return * true is the cell is empty and has no style or has no background fill. */ public static boolean cellIsEmpty(Cell cell) { if( cell.getCellType() != Cell.CELL_TYPE_BLANK ) { return false; } CellStyle cellStyle = cell.getCellStyle(); if( cellStyle == null ) { return true; } if( cellStyle.getFillPattern() == CellStyle.NO_FILL ) { return true; } return false; }
Example 12
Source File: AbstractSheet.java From tools with Apache License 2.0 | 5 votes |
/** * @param cell * @return */ @SuppressWarnings("deprecation") private int getNumWrappedLines(Cell cell) { if (cell.getCellTypeEnum() == CellType.STRING) { String val = cell.getStringCellValue(); if (val == null || val.isEmpty()) { return 1; } CellStyle style = cell.getCellStyle(); if (style == null || !style.getWrapText()) { return 1; } Font font = sheet.getWorkbook().getFontAt(style.getFontIndex()); AttributedString astr = new AttributedString(val); java.awt.Font awtFont = new java.awt.Font(font.getFontName(), 0, font.getFontHeightInPoints()); float cellWidth = sheet.getColumnWidth(cell.getColumnIndex())/ 256F * 5.5F; astr.addAttribute(TextAttribute.FONT, awtFont); FontRenderContext context = new FontRenderContext(null, true, true); java.awt.font.LineBreakMeasurer measurer = new java.awt.font.LineBreakMeasurer(astr.getIterator(), context); int pos = 0; int numLines = 0; while (measurer.getPosition() < val.length()) { pos = measurer.nextOffset(cellWidth); numLines++; measurer.setPosition(pos); } return numLines; } else { // Not a string type return 1; } }
Example 13
Source File: ExcelToHtmlServer.java From easypoi with Apache License 2.0 | 5 votes |
private void printSheetContent(Sheet sheet) { //printColumnHeads(sheet); MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet); CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom); out.format("<tbody>%n"); Iterator<Row> rows = sheet.rowIterator(); int rowIndex = 1; while (rows.hasNext()) { Row row = rows.next(); out.format(" <tr style='height:%spx;'>%n", row.getHeight() / 15); //out.format(" <td class='%s'>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1); for (int i = firstColumn; i < endColumn; i++) { if (mergedRegionHelper.isNeedCreate(rowIndex, i)) { String content = " "; CellStyle style = null; if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) { Cell cell = row.getCell(i); if (cell != null) { style = cell.getCellStyle(); content = cellValueHelper.getHtmlValue(cell); } } if (mergedRegionHelper.isMergedRegion(rowIndex, i)) { Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i); out.format(" <td rowspan='%s' colspan='%s' class='%s' >%s</td>%n", rowAndColSpan[0], rowAndColSpan[1], styleName(style), content); } else { out.format(" <td class='%s'>%s</td>%n", styleName(style), content); } } } out.format(" </tr>%n"); rowIndex++; } out.format("</tbody>%n"); }
Example 14
Source File: POIUtils.java From xlsmapper with Apache License 2.0 | 5 votes |
/** * 結合を考慮してセルの罫線(左部)を取得する。 * * @param cell セル * @return {@literal BorderStyle} * @throws IllegalArgumentException {@literal cell is null.} */ public static BorderStyle getBorderRight(final Cell cell) { ArgUtils.notNull(cell, "cell"); final Sheet sheet = cell.getSheet(); CellRangeAddress mergedRegion = getMergedRegion(sheet, cell.getRowIndex(), cell.getColumnIndex()); final Cell target; if(mergedRegion == null) { // 結合されていない場合 target = cell; } else { if(mergedRegion.getLastColumn() == cell.getColumnIndex()) { // 引数のCellが右部のセルの場合 target = cell; } else { target = getCell(sheet, mergedRegion.getLastColumn(), cell.getRowIndex()); } } final CellStyle style = target.getCellStyle(); if(style == null) { return BorderStyle.NONE; } else { return style.getBorderRightEnum(); } }
Example 15
Source File: HorizontalRecordsProcessor.java From xlsmapper with Apache License 2.0 | 5 votes |
/** * ネストしたレコードの親のセルを結合する * @param sheet シート * @param mergedSize 結合するセルのサイズ * @param valueCellPositions 結合する開始位置のセルのアドレス */ private void processSavingNestedMergedRecord(final Sheet sheet, final int mergedSize, final List<CellPosition> valueCellPositions) { if(mergedSize <= 1) { return; } // ネストした場合、上のセルのスタイルをコピーして、結合する for(CellPosition position : valueCellPositions) { Cell valueCell = POIUtils.getCell(sheet, position); if(valueCell == null) { continue; } final CellStyle style = valueCell.getCellStyle(); // 結合するセルに対して、上のセルのスタイルをコピーする。 // 行を挿入するときなどに必要になるため、スタイルを設定する。 for(int i=1; i < mergedSize; i++) { Cell mergedCell = POIUtils.getCell(sheet, position.getColumn(), position.getRow() + i); mergedCell.setCellStyle(style); mergedCell.setCellType(CellType.BLANK); } final CellRangeAddress range = new CellRangeAddress(position.getRow(), position.getRow()+ mergedSize-1, position.getColumn(), position.getColumn()); // 既に結合済みのセルがある場合、外す。 for(int rowIdx=range.getFirstRow(); rowIdx <= range.getLastRow(); rowIdx++) { CellRangeAddress r = POIUtils.getMergedRegion(sheet, rowIdx, position.getColumn()); if(r != null) { POIUtils.removeMergedRange(sheet, r); } } sheet.addMergedRegion(range); } }
Example 16
Source File: HorizontalRecordsProcessor.java From xlsmapper with Apache License 2.0 | 5 votes |
/** * セルの書式をコピーする。 * <p>コピー先のセルの種類は、空セルとする。</p> * <p>結合情報も列方向の結合をコピーする。</p> * * @since 2.0 * @param fromCell コピー元 * @param toCell コピー先 */ private void copyCellStyle(final Cell fromCell, final Cell toCell) { final CellStyle style = fromCell.getCellStyle(); toCell.setCellStyle(style); toCell.setCellType(CellType.BLANK); // 横方向に結合されている場合、結合情報のコピーする。(XlsArrayColumns用) final Sheet sheet = fromCell.getSheet(); final CellRangeAddress mergedRegion = POIUtils.getMergedRegion(sheet, fromCell.getRowIndex(), fromCell.getColumnIndex()); final int mergedSize = POIUtils.getColumnSize(mergedRegion); if(POIUtils.getColumnSize(mergedRegion) >= 2) { CellRangeAddress newMergedRegion = POIUtils.getMergedRegion(sheet, toCell.getRowIndex(), toCell.getColumnIndex()); if(newMergedRegion != null) { // 既に結合している場合 - 通常はありえない。 return; } newMergedRegion = POIUtils.mergeCells(sheet, mergedRegion.getFirstColumn(), toCell.getRowIndex(), mergedRegion.getLastColumn(), toCell.getRowIndex()); // 結合先のセルの書式も設定する // 中間のセルの設定 for(int i=1; i < mergedSize; i++) { Cell mergedFromCell = POIUtils.getCell(sheet, toCell.getColumnIndex()+i, fromCell.getRowIndex()); Cell mergedToCell = POIUtils.getCell(sheet, toCell.getColumnIndex()+i, toCell.getRowIndex()); mergedToCell.setCellStyle(mergedFromCell.getCellStyle()); mergedToCell.setCellType(CellType.BLANK); } } }
Example 17
Source File: ExcelToHtmlServer.java From autopoi with Apache License 2.0 | 5 votes |
private void printSheetContent(Sheet sheet) { // printColumnHeads(sheet); MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet); CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom); out.format("<tbody>%n"); Iterator<Row> rows = sheet.rowIterator(); int rowIndex = 1; while (rows.hasNext()) { Row row = rows.next(); out.format(" <tr style='height:%spx;'>%n", row.getHeight() / 15); // out.format(" <td class='%s'>%d</td>%n", ROW_HEAD_CLASS, // row.getRowNum() + 1); for (int i = firstColumn; i < endColumn; i++) { if (mergedRegionHelper.isNeedCreate(rowIndex, i)) { String content = " "; CellStyle style = null; if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) { Cell cell = row.getCell(i); if (cell != null) { style = cell.getCellStyle(); content = cellValueHelper.getHtmlValue(cell); } } if (mergedRegionHelper.isMergedRegion(rowIndex, i)) { Integer[] rowAndColSpan = mergedRegionHelper.getRowAndColSpan(rowIndex, i); out.format(" <td rowspan='%s' colspan='%s' class='%s' >%s</td>%n", rowAndColSpan[0], rowAndColSpan[1], styleName(style), content); } else { out.format(" <td class='%s'>%s</td>%n", styleName(style), content); } } } out.format(" </tr>%n"); rowIndex++; } out.format("</tbody>%n"); }
Example 18
Source File: CellValueFormatter.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 4 votes |
private String getFormattedNumberString(Cell cell) { CellStyle cellStyle = cell.getCellStyle(); return formatRawCellContents(cell.getNumericCellValue(), cellStyle.getDataFormat(), cellStyle.getDataFormatString(), false); }
Example 19
Source File: ExcelWriter_StyleFormatTest.java From hop with Apache License 2.0 | 4 votes |
/** * Test applying Format and Style from cell (from a template) when writing fields * * @param fileType * @throws Exception */ private void testStyleFormat( String fileType ) throws Exception { setupTransformMock( fileType ); createTransformMeta( fileType ); createTransformData( fileType ); transform.init(); // We do not run pipeline or executing the whole transform // instead we just execute ExcelWriterData.writeNextLine() to write to Excel workbook object // Values are written in A2:D2 and A3:D3 rows List<Object[]> rows = createRowData(); for ( int i = 0; i < rows.size(); i++ ) { transform.writeNextLine( rows.get( i ) ); } // Custom styles are loaded from G1 cell Row xlsRow = transformData.sheet.getRow( 0 ); Cell baseCell = xlsRow.getCell( 6 ); CellStyle baseCellStyle = baseCell.getCellStyle(); DataFormat format = transformData.wb.createDataFormat(); // Check style of the exported values in A3:D3 xlsRow = transformData.sheet.getRow( 2 ); for ( int i = 0; i < transformData.inputRowMeta.size(); i++ ) { Cell cell = xlsRow.getCell( i ); CellStyle cellStyle = cell.getCellStyle(); if ( i > 0 ) { assertEquals( cellStyle.getBorderRight(), baseCellStyle.getBorderRight() ); assertEquals( cellStyle.getFillPattern(), baseCellStyle.getFillPattern() ); } else { // cell A2/A3 has no custom style assertFalse( cellStyle.getBorderRight() == baseCellStyle.getBorderRight() ); assertFalse( cellStyle.getFillPattern() == baseCellStyle.getFillPattern() ); } if ( i != 1 ) { assertEquals( format.getFormat( cellStyle.getDataFormat() ), "0.00000" ); } else { // cell B2/B3 use different format from the custom style assertEquals( format.getFormat( cellStyle.getDataFormat() ), "##0,000.0" ); } } }
Example 20
Source File: ExcelWriterTransform_StyleFormatTest.java From hop with Apache License 2.0 | 4 votes |
/** * Test applying Format and Style from cell (from a template) when writing fields * * @param fileType * @throws Exception */ private void testStyleFormat( String fileType ) throws Exception { setupInputOutput( fileType ); createTransformMeta( fileType ); createTransformData( fileType ); setupTransformMock( fileType ); transform.init(); // We do not run pipeline or executing the whole transform // instead we just execute ExcelWriterTransformData.writeNextLine() to write to Excel workbook object // Values are written in A2:D2 and A3:D3 rows List<Object[]> rows = createRowData(); for ( int i = 0; i < rows.size(); i++ ) { transform.writeNextLine( rows.get( i ) ); } // Custom styles are loaded from G1 cell Row xlsRow = data.sheet.getRow( 0 ); Cell baseCell = xlsRow.getCell( 6 ); CellStyle baseCellStyle = baseCell.getCellStyle(); DataFormat format = data.wb.createDataFormat(); // Check style of the exported values in A3:D3 xlsRow = data.sheet.getRow( 2 ); for ( int i = 0; i < data.inputRowMeta.size(); i++ ) { Cell cell = xlsRow.getCell( i ); CellStyle cellStyle = cell.getCellStyle(); if ( i > 0 ) { assertEquals( cellStyle.getBorderRight(), baseCellStyle.getBorderRight() ); assertEquals( cellStyle.getFillPattern(), baseCellStyle.getFillPattern() ); } else { // cell A2/A3 has no custom style assertFalse( cellStyle.getBorderRight() == baseCellStyle.getBorderRight() ); assertFalse( cellStyle.getFillPattern() == baseCellStyle.getFillPattern() ); } if ( i != 1 ) { assertEquals( format.getFormat( cellStyle.getDataFormat() ), "0.00000" ); } else { // cell B2/B3 use different format from the custom style assertEquals( format.getFormat( cellStyle.getDataFormat() ), "##0,000.0" ); } } }