List of usage examples for org.apache.poi.ss.usermodel CellStyle getWrapText
boolean getWrapText();
From source file:com.canoo.webtest.plugins.exceltest.ExcelVerifyCellStyle.java
License:Open Source License
public void doExecute() throws Exception { final String[] border = separateSides(getBorder()); final String[] borderColor = separateSides(getBorderColor()); for (int i = 0; i < SIDES.length; i++) { checkFormat(SIDES[i] + "Border", border[i], getCellBorder(i)); checkFormat(SIDES[i] + "BorderColor", ExcelColorUtils.lookupStandardColorName(borderColor[i]), getCellBorderColor(i));//from w w w . j av a 2s.co m } final Cell excelCell = getExcelCell(); checkFormat("type", getType(), ExcelCellUtils.getCellType(excelCell == null ? Cell.CELL_TYPE_BLANK : excelCell.getCellType())); if (excelCell == null) { if (cellNotRequired()) { return; } else { throw new StepExecutionException("Can't find cell for " + getCellReferenceStr(), this); } } final CellStyle cellStyle = excelCell.getCellStyle(); checkFormat("format", getFormat(), getExcelWorkbook().createDataFormat().getFormat(cellStyle.getDataFormat())); checkFormat("align", getAlign(), ExcelCellUtils.getAlignmentString(cellStyle.getAlignment())); checkFormat("valign", getValign(), ExcelCellUtils.getVerticalAlignmentString(cellStyle.getVerticalAlignment())); checkFormat("wrap", getWrap(), String.valueOf(cellStyle.getWrapText())); checkFormat("locked", getLocked(), String.valueOf(cellStyle.getLocked())); checkFormat("fontName", getFontName(), getFont(cellStyle).getFontName()); checkFormat("fontSize", getFontSize(), String.valueOf(getFont(cellStyle).getFontHeightInPoints())); checkFormat("fontStyle", sortElements(getFontStyle()), getFontStyle(getFont(cellStyle))); checkFormat("fillColor", ExcelColorUtils.lookupStandardColorName(getFillColor()), ExcelColorUtils.getColorName(this, cellStyle.getFillForegroundColorColor())); checkFormat("fillBackgroundColor", ExcelColorUtils.lookupStandardColorName(getFillBackgroundColor()), ExcelColorUtils.getColorName(this, cellStyle.getFillBackgroundColorColor())); checkFormat("textColor", ExcelColorUtils.lookupStandardColorName(getTextColor()), ExcelColorUtils.getColorName(this, getFont(cellStyle).getColor())); checkFormat("fillPattern", getFillPattern(), ExcelCellUtils.getFillPattern(cellStyle.getFillPattern())); }
From source file:com.ncc.excel.test.ExcelUtil.java
License:Apache License
/** * ????? // www. j a va 2s . c o m * * @param fromStyle * @param toStyle */ public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) { toStyle.setAlignment(fromStyle.getAlignment()); // toStyle.setBorderBottom(fromStyle.getBorderBottom()); toStyle.setBorderLeft(fromStyle.getBorderLeft()); toStyle.setBorderRight(fromStyle.getBorderRight()); toStyle.setBorderTop(fromStyle.getBorderTop()); toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); // ? toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); // ?? toStyle.setDataFormat(fromStyle.getDataFormat()); toStyle.setFillPattern(fromStyle.getFillPattern()); // toStyle.setFont(fromStyle.getFont(null)); toStyle.setHidden(fromStyle.getHidden()); toStyle.setIndention(fromStyle.getIndention());// toStyle.setLocked(fromStyle.getLocked()); toStyle.setRotation(fromStyle.getRotation());// toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); toStyle.setWrapText(fromStyle.getWrapText()); }
From source file:com.vaadin.addon.spreadsheet.CellValueManager.java
License:Open Source License
protected CellData createCellDataForCell(Cell cell) { CellData cellData = new CellData(); cellData.row = cell.getRowIndex() + 1; cellData.col = cell.getColumnIndex() + 1; CellStyle cellStyle = cell.getCellStyle(); cellData.cellStyle = "cs" + cellStyle.getIndex(); cellData.locked = spreadsheet.isCellLocked(cell); try {//w ww. ja v a 2 s . c o m if (!spreadsheet.isCellHidden(cell)) { if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { cellData.formulaValue = formulaFormatter.reFormatFormulaValue(cell.getCellFormula(), spreadsheet.getLocale()); try { String oldValue = getCachedFormulaCellValue(cell); String newValue = formatter.formatCellValue(cell, getFormulaEvaluator()); if (!newValue.equals(oldValue)) { changedFormulaCells.add(new CellReference(cell)); } } catch (RuntimeException rte) { // Apache POI throws RuntimeExceptions for an invalid // formula from POI model String formulaValue = cell.getCellFormula(); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(formulaValue); spreadsheet.markInvalidFormula(cell.getColumnIndex() + 1, cell.getRowIndex() + 1); } } } if (cell.getCellStyle().getDataFormatString().contains("%")) { cellData.isPercentage = true; } String formattedCellValue = formatter.formatCellValue(cell, getFormulaEvaluator()); if (!spreadsheet.isCellHidden(cell)) { if (cell.getCellType() == Cell.CELL_TYPE_FORMULA || cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { formattedCellValue = formattedCellValue.replaceAll("^-(?=0(.0*)?$)", ""); } } if (spreadsheet.isMarkedAsInvalidFormula(cellData.col, cellData.row)) { // The prefix '=' or '+' should not be included in formula value if (cell.getStringCellValue().charAt(0) == '+' || cell.getStringCellValue().charAt(0) == '=') { cellData.formulaValue = cell.getStringCellValue().substring(1); } formattedCellValue = "#VALUE!"; } if (formattedCellValue != null && !formattedCellValue.isEmpty() || cellStyle.getIndex() != 0) { // if the cell is not wrapping text, and is of type numeric or // formula (but not date), calculate if formatted cell value // fits the column width and possibly use scientific notation. cellData.value = formattedCellValue; cellData.needsMeasure = false; if (!cellStyle.getWrapText() && (!SpreadsheetUtil.cellContainsDate(cell) && cell.getCellType() == Cell.CELL_TYPE_NUMERIC || cell.getCellType() == Cell.CELL_TYPE_STRING || (cell.getCellType() == Cell.CELL_TYPE_FORMULA && !cell.getCellFormula().startsWith("HYPERLINK")))) { if (!doesValueFit(cell, formattedCellValue)) { if (valueContainsOnlyNumbers(formattedCellValue) && isGenerallCell(cell)) { cellData.value = cellValueFormatter.getScientificNotationStringForNumericCell( cell.getNumericCellValue(), formattedCellValue, cellStyleWidthRatioMap.get((int) cell.getCellStyle().getIndex()), spreadsheet.getState(false).colW[cell.getColumnIndex()] - 10); } else if (cell.getCellType() != Cell.CELL_TYPE_STRING) { cellData.needsMeasure = true; } } } if (cellStyle.getAlignment() == CellStyle.ALIGN_RIGHT) { cellData.cellStyle = cellData.cellStyle + " r"; } else if (cellStyle.getAlignment() == CellStyle.ALIGN_GENERAL) { if (SpreadsheetUtil.cellContainsDate(cell) || cell.getCellType() == Cell.CELL_TYPE_NUMERIC || (cell.getCellType() == Cell.CELL_TYPE_FORMULA && !cell.getCellFormula().startsWith("HYPERLINK") && !(cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING))) { cellData.cellStyle = cellData.cellStyle + " r"; } } } // conditional formatting might be applied even if there isn't a // value (such as borders for the cell to the right) Set<Integer> cellFormattingIndexes = spreadsheet.getConditionalFormatter().getCellFormattingIndex(cell); if (cellFormattingIndexes != null) { for (Integer i : cellFormattingIndexes) { cellData.cellStyle = cellData.cellStyle + " cf" + i; } markedCells.add(SpreadsheetUtil.toKey(cell)); } if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && DateUtil.isCellDateFormatted(cell)) { cellData.originalValue = cellData.value; } else { cellData.originalValue = getOriginalCellValue(cell); } handleIsDisplayZeroPreference(cell, cellData); } catch (RuntimeException rte) { LOGGER.log(Level.FINEST, rte.getMessage(), rte); cellData.value = "#VALUE!"; } return cellData; }
From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java
License:Open Source License
private void addCellStyleCSS(CellStyle cellStyle) { if (cellStyle.getIndex() == 0) { // default cell style, do not change. return;//from w ww .j av a 2s .c om } StringBuilder sb = new StringBuilder(); fontStyle(sb, cellStyle); colorConverter.colorStyles(cellStyle, sb); borderStyles(sb, cellStyle); if (cellStyle.getAlignment() != defaultTextAlign) { styleOut(sb, "text-align", cellStyle.getAlignment(), ALIGN); // TODO For correct overflow, rtl should be used for right align // if (cellStyle.getAlignment() == ALIGN_RIGHT) { // sb.append("direction:rtl;"); // } } // excel default is bottom, so that is what we have in the CSS base // files. // TODO This only works on modern (10+) IE. styleOut(sb, "justify-content", cellStyle.getVerticalAlignment(), VERTICAL_ALIGN); if (cellStyle.getWrapText()) { // default is to overflow sb.append("overflow:hidden;white-space:normal;"); } if (cellStyle.getIndention() > 0) { sb.append("padding-left: " + cellStyle.getIndention() + "em;"); } spreadsheet.getState().cellStyleToCSSStyle.put((int) cellStyle.getIndex(), sb.toString()); }
From source file:egovframework.rte.fdl.excel.EgovExcelServiceTest.java
License:Apache License
/** * [Flow #-6] ? : ? ?(?, ? )? /*from w w w.j ava2s .c om*/ */ @Test public void testModifyCellAttribute() throws Exception { try { LOGGER.debug("testModifyCellAttribute start...."); StringBuffer sb = new StringBuffer(); sb.append(fileLocation).append("/").append("testModifyCellAttribute.xls"); if (EgovFileUtil.isExistsFile(sb.toString())) { EgovFileUtil.delete(new File(sb.toString())); LOGGER.debug("Delete file....{}", sb.toString()); } Workbook wbTmp = new HSSFWorkbook(); wbTmp.createSheet(); // ? ? excelService.createWorkbook(wbTmp, sb.toString()); // ? Workbook wb = excelService.loadWorkbook(sb.toString()); LOGGER.debug("testModifyCellAttribute after loadWorkbook...."); Sheet sheet = wb.createSheet("cell test sheet2"); // sheet.setColumnWidth((short) 3, (short) 200); // column Width CellStyle cs = wb.createCellStyle(); Font font = wb.createFont(); font.setFontHeight((short) 16); font.setBoldweight((short) 3); font.setFontName("fixedsys"); cs.setFont(font); cs.setAlignment(CellStyle.ALIGN_RIGHT); // cell cs.setWrapText(true); for (int i = 0; i < 100; i++) { Row row = sheet.createRow(i); // row.setHeight((short)300); // row? height for (int j = 0; j < 5; j++) { Cell cell = row.createCell(j); cell.setCellValue(new HSSFRichTextString("row " + i + ", cell " + j)); cell.setCellStyle(cs); } } // ? FileOutputStream out = new FileOutputStream(sb.toString()); wb.write(out); out.close(); ////////////////////////////////////////////////////////////////////////// // ? Workbook wbT = excelService.loadWorkbook(sb.toString()); Sheet sheetT = wbT.getSheet("cell test sheet2"); LOGGER.debug("getNumCellStyles : {}", wbT.getNumCellStyles()); CellStyle cs1 = wbT.getCellStyleAt((short) (wbT.getNumCellStyles() - 1)); Font fontT = ((HSSFCellStyle) cs1).getFont(wbT); LOGGER.debug("font getFontHeight : {}", fontT.getFontHeight()); LOGGER.debug("font getBoldweight : {}", fontT.getBoldweight()); LOGGER.debug("font getFontName : {}", fontT.getFontName()); LOGGER.debug("getAlignment : {}", cs1.getAlignment()); LOGGER.debug("getWrapText : {}", cs1.getWrapText()); for (int i = 0; i < 100; i++) { Row row1 = sheetT.getRow(i); for (int j = 0; j < 5; j++) { Cell cell1 = row1.getCell(j); LOGGER.debug("row {}, cell {} : {}", i, j, cell1.getRichStringCellValue()); assertEquals(16, fontT.getFontHeight()); assertEquals(3, fontT.getBoldweight()); assertEquals(CellStyle.ALIGN_RIGHT, cs1.getAlignment()); assertTrue(cs1.getWrapText()); } } } catch (Exception e) { LOGGER.error(e.toString()); throw new Exception(e); } finally { LOGGER.debug("testModifyCellAttribute end...."); } }
From source file:net.ceos.project.poi.annotated.core.CellStyleHandler.java
License:Apache License
/** * Clone a cell style passed as parameter. * /*from www .j a v a 2 s . c om*/ * @param wb * the {@link Workbook} in use * @param csBase * the {@link CellStyle} base * @return the new cell style */ private static CellStyle cloneCellStyle(final Workbook wb, final CellStyle csBase) { CellStyle cs = cellStyleFactory.newInstance(wb); cs.setAlignment(csBase.getAlignment()); cs.setVerticalAlignment(csBase.getVerticalAlignment()); cs.setBorderTop(csBase.getBorderTop()); cs.setBorderBottom(csBase.getBorderBottom()); cs.setBorderLeft(csBase.getBorderLeft()); cs.setBorderRight(csBase.getBorderRight()); cs.setFillForegroundColor(csBase.getFillForegroundColor()); cs.setFillPattern(csBase.getFillPattern()); cs.setWrapText(csBase.getWrapText()); cs.setFont(wb.getFontAt(csBase.getFontIndex())); return cs; }
From source file:org.drugepi.table.CellStyleLookup.java
License:Mozilla Public License
public static String styleToString(CellStyle style) { StringBuffer sb = new StringBuffer(); sb.append("getDataFormatString=" + style.getDataFormatString() + "\n"); sb.append("getFontIndex=" + style.getFontIndex() + "\n"); sb.append("getHidden=" + style.getHidden() + "\n"); sb.append("getAlignment=" + style.getAlignment() + "\n"); sb.append("getWrapText=" + style.getWrapText() + "\n"); sb.append("getVerticalAlignment=" + style.getVerticalAlignment() + "\n"); sb.append("getRotation=" + style.getRotation() + "\n"); sb.append("getIndention=" + style.getIndention() + "\n"); sb.append("getBorderLeft=" + style.getBorderLeft() + "\n"); sb.append("getBorderRight=" + style.getBorderRight() + "\n"); sb.append("getBorderTop=" + style.getBorderTop() + "\n"); sb.append("getBorderBottom=" + style.getBorderBottom() + "\n"); sb.append("getLeftBorderColor=" + style.getLeftBorderColor() + "\n"); sb.append("getRightBorderColor=" + style.getRightBorderColor() + "\n"); sb.append("getTopBorderColor=" + style.getTopBorderColor() + "\n"); sb.append("getBottomBorderColor=" + style.getBottomBorderColor() + "\n"); sb.append("getFillPattern=" + style.getFillPattern() + "\n"); sb.append("getFillBackgroundColor=" + style.getFillBackgroundColor() + "\n"); sb.append("getFillForegroundColor=" + style.getFillForegroundColor() + "\n"); return sb.toString(); }
From source file:org.netxilia.impexp.impl.PoiUtils.java
License:Open Source License
public static Styles poiStyle2Netxilia(CellStyle poiStyle, Font font, HSSFPalette palette, NetxiliaStyleResolver styleResolver) { List<Style> entries = new ArrayList<Style>(); if (!poiStyle.getWrapText()) { entries.add(DefaultStyle.nowrap.getStyle()); }// ww w .ja va 2s . c om // font if (font.getItalic()) { entries.add(DefaultStyle.italic.getStyle()); } if (font.getStrikeout()) { entries.add(DefaultStyle.strikeout.getStyle()); } if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) { entries.add(DefaultStyle.bold.getStyle()); } if (font.getUnderline() != Font.U_NONE) { entries.add(DefaultStyle.underline.getStyle()); } // borders if (poiStyle.getBorderBottom() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderBottom.getStyle()); } if (poiStyle.getBorderLeft() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderLeft.getStyle()); } if (poiStyle.getBorderTop() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderTop.getStyle()); } if (poiStyle.getBorderRight() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderRight.getStyle()); } // align switch (poiStyle.getAlignment()) { case CellStyle.ALIGN_LEFT: entries.add(DefaultStyle.alignLeft.getStyle()); break; case CellStyle.ALIGN_RIGHT: entries.add(DefaultStyle.alignRight.getStyle()); break; case CellStyle.ALIGN_CENTER: entries.add(DefaultStyle.alignCenter.getStyle()); break; case CellStyle.ALIGN_JUSTIFY: entries.add(DefaultStyle.alignJustify.getStyle()); break; } if (font != null && font.getColor() != 0) { HSSFColor poiForeground = palette.getColor(font.getColor()); if (poiForeground != null && poiForeground != HSSFColor.AUTOMATIC.getInstance()) { Style foregroundDef = styleResolver.approximateForeground(poiForeground.getTriplet()[0], poiForeground.getTriplet()[1], poiForeground.getTriplet()[2]); if (foregroundDef != null) { entries.add(foregroundDef); } } } if (poiStyle.getFillForegroundColor() != 0) { HSSFColor poiBackground = palette.getColor(poiStyle.getFillForegroundColor()); if (poiBackground != null && poiBackground != HSSFColor.AUTOMATIC.getInstance()) { Style backgroundDef = styleResolver.approximateBackground(poiBackground.getTriplet()[0], poiBackground.getTriplet()[1], poiBackground.getTriplet()[2]); if (backgroundDef != null) { entries.add(backgroundDef); } } } return entries.size() > 0 ? Styles.styles(entries) : null; }
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private void fillCellStyle(CStyle cellStyle, CellStyle excelCellStyle) { Font excelFont = workbook.getFontAt(excelCellStyle.getFontIndex()); // TODO CFont newFont(excelFont) //CFont font = new CFont(); //cellStyle.setFont( font ); CFont font = cellStyle.getFont();/*from w w w . j a v a 2 s . com*/ fillFont(font, excelFont); cellStyle.setHidden(excelCellStyle.getHidden()); cellStyle.setLocked(excelCellStyle.getLocked()); cellStyle.setWrapped(excelCellStyle.getWrapText()); cellStyle.setIndention(excelCellStyle.getIndention()); cellStyle.setRotation(excelCellStyle.getRotation()); cellStyle.setHorzAlignment(this.getHorzAlignment(excelCellStyle.getAlignment())); cellStyle.setVertAlignment(this.getVertAlignment(excelCellStyle.getVerticalAlignment())); CBorder leftBorder = cellStyle.getLeftBorder(); CBorder rightBorder = cellStyle.getRightBorder(); CBorder topBorder = cellStyle.getTopBorder(); CBorder bottomBorder = cellStyle.getBottomBorder(); BorderType lbType = this.convertBorderType(excelCellStyle.getBorderLeft()); BorderType rbType = this.convertBorderType(excelCellStyle.getBorderRight()); BorderType tbType = this.convertBorderType(excelCellStyle.getBorderTop()); BorderType bbType = this.convertBorderType(excelCellStyle.getBorderBottom()); leftBorder.setType(lbType); rightBorder.setType(rbType); topBorder.setType(tbType); bottomBorder.setType(bbType); // "Fill Background Color" ???, ??, // ? ??? . ? . // "Fill Foreground Color" XSSFColor bgColor = (XSSFColor) excelCellStyle.getFillBackgroundColorColor(); // ? Index 64, ? , , // ? ? null ? if (null != bgColor && 64 != bgColor.getIndexed()) { String bgColorHexRGB = bgColor.getARGBHex().substring(2); cellStyle.setBgColor(new CColor(bgColorHexRGB)); } // "Fill Background Color" ??, // ? ??? . XSSFColor fgColor = (XSSFColor) excelCellStyle.getFillForegroundColorColor(); if (null != fgColor && 64 != fgColor.getIndexed()) { String fgColorHexRGB = fgColor.getARGBHex().substring(2); cellStyle.setFgColor(new CColor(fgColorHexRGB)); } // TODO }