List of usage examples for org.apache.poi.hssf.usermodel HSSFCellStyle getAlignment
@Override
public HorizontalAlignment getAlignment()
From source file:at.spardat.xma.mdl.grid.GridPOIAdapter.java
License:Open Source License
/** * Calculates the alignement of each column of the given grid. * In Excel alignements are defined per cell, in SWT alignement are defined per column. * So the alignment for SWT is calculated by choosing the most used alignement of the visible * cells of each column./*from w ww. ja v a 2 s .co m*/ * * @param igrid the XMA model where to set the alignemnets * @param sheet the POI representation from where to read the alignements */ static public void calcAlignements(IGridWM igrid, HSSFSheet sheet) { GridWM grid = (GridWM) igrid; GridRange range = grid.getVisibleRange(); for (int col = range.getFirstColumn(), lastCol = range.getLastColumn(); col <= lastCol; col++) { GridColumn gridColumn = grid.getOrCreateColumn(col); if (gridColumn.isHidden()) continue; int left = 0, right = 0, center = 0; for (int i = range.getFirstRow(), lastRow = range.getLastRow(); i <= lastRow; i++) { GridRow gridRow = grid.getRow(i); if (gridRow != null && gridRow.isHidden()) continue; HSSFRow hrow = sheet.getRow(i); if (hrow == null) continue; HSSFCell hcell = hrow.getCell((short) col); if (hcell == null) continue; HSSFCellStyle hstyle = hcell.getCellStyle(); if (hstyle == null) continue; switch (hstyle.getAlignment()) { case HSSFCellStyle.ALIGN_CENTER: case HSSFCellStyle.ALIGN_CENTER_SELECTION: center++; break; case HSSFCellStyle.ALIGN_LEFT: case HSSFCellStyle.ALIGN_FILL: case HSSFCellStyle.ALIGN_JUSTIFY: left++; break; case HSSFCellStyle.ALIGN_RIGHT: right++; break; case HSSFCellStyle.ALIGN_GENERAL: switch (hcell.getCellType()) { case HSSFCell.CELL_TYPE_BOOLEAN: center++; break; case HSSFCell.CELL_TYPE_NUMERIC: case 42: // CELL_TYPE_DATE: right++; break; case HSSFCell.CELL_TYPE_STRING: left++; break; case HSSFCell.CELL_TYPE_BLANK: case HSSFCell.CELL_TYPE_ERROR: case HSSFCell.CELL_TYPE_FORMULA: default: break; } break; default: break; } } if (left >= right && left >= center) { gridColumn.setAlignement(GridColumn.ALIGN_LEFT); } else if (right > left && right >= center) { gridColumn.setAlignement(GridColumn.ALIGN_RIGHT); } else if (center > left && center > right) { gridColumn.setAlignement(GridColumn.ALIGN_CENTER); } } }
From source file:cn.trymore.core.util.excel.PoiExcelParser.java
License:Open Source License
public String getCellAlignment(HSSFCell cell) { HSSFCellStyle style = cell.getCellStyle(); if ((cell != null) && (style != null)) { switch (style.getAlignment()) { case 2://w w w . j ava 2 s . c o m return "text-align:center;"; case 1: return "text-align:left;"; case 3: return "text-align:right;"; } } return ""; }
From source file:com.develog.utils.report.engine.export.JRXlsExporter.java
License:Open Source License
/** * */// w w w.j av a 2 s .c o m protected HSSFCellStyle getLoadedCellStyle(short mode, short backcolor, short horizontalAlignment, short verticalAlignment, short rotation, HSSFFont font) { HSSFCellStyle cellStyle = null; if (loadedCellStyles != null && loadedCellStyles.size() > 0) { HSSFCellStyle cs = null; for (int i = 0; i < loadedCellStyles.size(); i++) { cs = (HSSFCellStyle) loadedCellStyles.get(i); if (cs.getFillPattern() == mode && cs.getFillForegroundColor() == backcolor && cs.getAlignment() == horizontalAlignment && cs.getVerticalAlignment() == verticalAlignment && cs.getRotation() == rotation && cs.getFontIndex() == font.getIndex()) { cellStyle = cs; break; } } } if (cellStyle == null) { cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(backcolor); cellStyle.setFillPattern(mode); cellStyle.setAlignment(horizontalAlignment); cellStyle.setVerticalAlignment(verticalAlignment); cellStyle.setRotation(rotation); cellStyle.setFont(font); cellStyle.setWrapText(true); loadedCellStyles.add(cellStyle); } return cellStyle; }
From source file:com.eryansky.core.excelTools.ExcelUtils.java
License:Apache License
public static String dumpCellStyle(HSSFCellStyle style) { StringBuffer sb = new StringBuffer(); sb.append(style.getHidden()).append(","); sb.append(style.getLocked()).append(","); sb.append(style.getWrapText()).append(","); sb.append(style.getAlignment()).append(","); sb.append(style.getBorderBottom()).append(","); sb.append(style.getBorderLeft()).append(","); sb.append(style.getBorderRight()).append(","); sb.append(style.getBorderTop()).append(","); sb.append(style.getBottomBorderColor()).append(","); sb.append(style.getDataFormat()).append(","); sb.append(style.getFillBackgroundColor()).append(","); sb.append(style.getFillForegroundColor()).append(","); sb.append(style.getFillPattern()).append(","); sb.append(style.getIndention()).append(","); sb.append(style.getLeftBorderColor()).append(","); sb.append(style.getRightBorderColor()).append(","); sb.append(style.getRotation()).append(","); sb.append(style.getTopBorderColor()).append(","); sb.append(style.getVerticalAlignment()); return sb.toString(); }
From source file:com.eryansky.core.excelTools.ExcelUtils.java
License:Apache License
public static HSSFCellStyle findStyle(HSSFCellStyle style, HSSFWorkbook srcwb, HSSFWorkbook destwb) { HSSFPalette srcpalette = srcwb.getCustomPalette(); HSSFPalette destpalette = destwb.getCustomPalette(); for (short i = 0; i < destwb.getNumCellStyles(); i++) { HSSFCellStyle old = destwb.getCellStyleAt(i); if (old == null) continue; if (style.getAlignment() == old.getAlignment() && style.getBorderBottom() == old.getBorderBottom() && style.getBorderLeft() == old.getBorderLeft() && style.getBorderRight() == old.getBorderRight() && style.getBorderTop() == old.getBorderTop() && isSameColor(style.getBottomBorderColor(), old.getBottomBorderColor(), srcpalette, destpalette)//from w w w.j a v a2 s . co m && style.getDataFormat() == old.getDataFormat() && isSameColor(style.getFillBackgroundColor(), old.getFillBackgroundColor(), srcpalette, destpalette) && isSameColor(style.getFillForegroundColor(), old.getFillForegroundColor(), srcpalette, destpalette) && style.getFillPattern() == old.getFillPattern() && style.getHidden() == old.getHidden() && style.getIndention() == old.getIndention() && isSameColor(style.getLeftBorderColor(), old.getLeftBorderColor(), srcpalette, destpalette) && style.getLocked() == old.getLocked() && isSameColor(style.getRightBorderColor(), old.getRightBorderColor(), srcpalette, destpalette) && style.getRotation() == old.getRotation() && isSameColor(style.getTopBorderColor(), old.getTopBorderColor(), srcpalette, destpalette) && style.getVerticalAlignment() == old.getVerticalAlignment() && style.getWrapText() == old.getWrapText()) { HSSFFont oldfont = destwb.getFontAt(old.getFontIndex()); HSSFFont font = srcwb.getFontAt(style.getFontIndex()); if (oldfont.getBoldweight() == font.getBoldweight() && oldfont.getItalic() == font.getItalic() && oldfont.getStrikeout() == font.getStrikeout() && oldfont.getCharSet() == font.getCharSet() && isSameColor(oldfont.getColor(), font.getColor(), srcpalette, destpalette) && oldfont.getFontHeight() == font.getFontHeight() && oldfont.getFontName().equals(font.getFontName()) && oldfont.getTypeOffset() == font.getTypeOffset() && oldfont.getUnderline() == font.getUnderline()) { return old; } } } return null; }
From source file:com.eryansky.core.excelTools.ExcelUtils.java
License:Apache License
public static void copyCellStyle(HSSFWorkbook destwb, HSSFCellStyle dest, HSSFWorkbook srcwb, HSSFCellStyle src) { if (src == null || dest == null) return;//from w w w . j a va 2 s. c o m dest.setAlignment(src.getAlignment()); dest.setBorderBottom(src.getBorderBottom()); dest.setBorderLeft(src.getBorderLeft()); dest.setBorderRight(src.getBorderRight()); dest.setBorderTop(src.getBorderTop()); dest.setBottomBorderColor(findColor(src.getBottomBorderColor(), srcwb, destwb)); dest.setDataFormat( destwb.createDataFormat().getFormat(srcwb.createDataFormat().getFormat(src.getDataFormat()))); dest.setFillPattern(src.getFillPattern()); dest.setFillForegroundColor(findColor(src.getFillForegroundColor(), srcwb, destwb)); dest.setFillBackgroundColor(findColor(src.getFillBackgroundColor(), srcwb, destwb)); dest.setHidden(src.getHidden()); dest.setIndention(src.getIndention()); dest.setLeftBorderColor(findColor(src.getLeftBorderColor(), srcwb, destwb)); dest.setLocked(src.getLocked()); dest.setRightBorderColor(findColor(src.getRightBorderColor(), srcwb, destwb)); dest.setRotation(src.getRotation()); dest.setTopBorderColor(findColor(src.getTopBorderColor(), srcwb, destwb)); dest.setVerticalAlignment(src.getVerticalAlignment()); dest.setWrapText(src.getWrapText()); HSSFFont f = srcwb.getFontAt(src.getFontIndex()); HSSFFont nf = findFont(f, srcwb, destwb); if (nf == null) { nf = destwb.createFont(); nf.setBoldweight(f.getBoldweight()); nf.setCharSet(f.getCharSet()); nf.setColor(findColor(f.getColor(), srcwb, destwb)); nf.setFontHeight(f.getFontHeight()); nf.setFontHeightInPoints(f.getFontHeightInPoints()); nf.setFontName(f.getFontName()); nf.setItalic(f.getItalic()); nf.setStrikeout(f.getStrikeout()); nf.setTypeOffset(f.getTypeOffset()); nf.setUnderline(f.getUnderline()); } dest.setFont(nf); }
From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java
License:Apache License
@Override public void apply() { for (DataObject dataObject : data) { HSSFCell templateCell = dataObject.templateCell; HSSFCell resultCell = dataObject.resultCell; BandData bandData = dataObject.bandData; HSSFWorkbook resultWorkbook = resultCell.getSheet().getWorkbook(); HSSFWorkbook templateWorkbook = templateCell.getSheet().getWorkbook(); String templateCellValue = templateCell.getStringCellValue(); Matcher matcher = pattern.matcher(templateCellValue); if (matcher.find()) { String paramName = matcher.group(1); String styleName = (String) bandData.getParameterValue(paramName); if (styleName == null) continue; HSSFCellStyle cellStyle = styleCache.getStyleByName(styleName); if (cellStyle == null) continue; HSSFCellStyle resultStyle = styleCache.getNamedCachedStyle(cellStyle); if (resultStyle == null) { HSSFCellStyle newStyle = resultWorkbook.createCellStyle(); // color newStyle.setFillBackgroundColor(cellStyle.getFillBackgroundColor()); newStyle.setFillForegroundColor(cellStyle.getFillForegroundColor()); newStyle.setFillPattern(cellStyle.getFillPattern()); // borders newStyle.setBorderLeft(cellStyle.getBorderLeft()); newStyle.setBorderRight(cellStyle.getBorderRight()); newStyle.setBorderTop(cellStyle.getBorderTop()); newStyle.setBorderBottom(cellStyle.getBorderBottom()); // border colors newStyle.setLeftBorderColor(cellStyle.getLeftBorderColor()); newStyle.setRightBorderColor(cellStyle.getRightBorderColor()); newStyle.setBottomBorderColor(cellStyle.getBottomBorderColor()); newStyle.setTopBorderColor(cellStyle.getTopBorderColor()); // alignment newStyle.setAlignment(cellStyle.getAlignment()); newStyle.setVerticalAlignment(cellStyle.getVerticalAlignment()); // misc DataFormat dataFormat = resultWorkbook.getCreationHelper().createDataFormat(); newStyle.setDataFormat(dataFormat.getFormat(cellStyle.getDataFormatString())); newStyle.setHidden(cellStyle.getHidden()); newStyle.setLocked(cellStyle.getLocked()); newStyle.setIndention(cellStyle.getIndention()); newStyle.setRotation(cellStyle.getRotation()); newStyle.setWrapText(cellStyle.getWrapText()); // font HSSFFont cellFont = cellStyle.getFont(templateWorkbook); HSSFFont newFont = fontCache.getFontByTemplate(cellFont); if (newFont == null) { newFont = resultWorkbook.createFont(); newFont.setFontName(cellFont.getFontName()); newFont.setItalic(cellFont.getItalic()); newFont.setStrikeout(cellFont.getStrikeout()); newFont.setTypeOffset(cellFont.getTypeOffset()); newFont.setBoldweight(cellFont.getBoldweight()); newFont.setCharSet(cellFont.getCharSet()); newFont.setColor(cellFont.getColor()); newFont.setUnderline(cellFont.getUnderline()); newFont.setFontHeight(cellFont.getFontHeight()); newFont.setFontHeightInPoints(cellFont.getFontHeightInPoints()); fontCache.addCachedFont(cellFont, newFont); }// ww w .ja v a2s . c o m newStyle.setFont(newFont); resultStyle = newStyle; styleCache.addCachedNamedStyle(cellStyle, resultStyle); } fixNeighbourCellBorders(cellStyle, resultCell); resultCell.setCellStyle(resultStyle); Sheet sheet = resultCell.getSheet(); for (int i = 0; i < sheet.getNumMergedRegions(); i++) { CellRangeAddress mergedRegion = sheet.getMergedRegion(i); if (mergedRegion.isInRange(resultCell.getRowIndex(), resultCell.getColumnIndex())) { int firstRow = mergedRegion.getFirstRow(); int lastRow = mergedRegion.getLastRow(); int firstCol = mergedRegion.getFirstColumn(); int lastCol = mergedRegion.getLastColumn(); for (int row = firstRow; row <= lastRow; row++) for (int col = firstCol; col <= lastCol; col++) sheet.getRow(row).getCell(col).setCellStyle(resultStyle); // cell includes only in one merged region break; } } } } }
From source file:com.report.excel.ExcelToHtmlConverter.java
License:Apache License
protected String buildStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle) { StringBuilder style = new StringBuilder(); style.append("white-space:pre-wrap;"); ExcelToHtmlUtils.appendAlign(style, cellStyle.getAlignment()); if (cellStyle.getFillPattern() == 0) { // no fill } else if (cellStyle.getFillPattern() == 1) { final HSSFColor foregroundColor = cellStyle.getFillForegroundColorColor(); if (foregroundColor != null) style.append("background-color:" + ExcelToHtmlUtils.getColor(foregroundColor) + ";"); } else {//from w w w. jav a 2 s. co m final HSSFColor backgroundColor = cellStyle.getFillBackgroundColorColor(); if (backgroundColor != null) style.append("background-color:" + ExcelToHtmlUtils.getColor(backgroundColor) + ";"); } buildStyle_border(workbook, style, "top", cellStyle.getBorderTop(), cellStyle.getTopBorderColor()); buildStyle_border(workbook, style, "right", cellStyle.getBorderRight(), cellStyle.getRightBorderColor()); buildStyle_border(workbook, style, "bottom", cellStyle.getBorderBottom(), cellStyle.getBottomBorderColor()); buildStyle_border(workbook, style, "left", cellStyle.getBorderLeft(), cellStyle.getLeftBorderColor()); HSSFFont font = cellStyle.getFont(workbook); buildStyle_font(workbook, style, font); return style.toString(); }
From source file:com.report.excel.ExcelToHtmlConverter.java
License:Apache License
protected boolean processCell(HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) { final HSSFCellStyle cellStyle = cell.getCellStyle(); String value;/*from w w w.j a v a2 s.c o m*/ switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: // XXX: enrich value = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_FORMULA: /*switch (evaluator.evaluateFormulaCell(cell)) { case Cell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_NUMERIC: value = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: System.out.println(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: break; }*/ switch (cell.getCachedFormulaResultType()) { case HSSFCell.CELL_TYPE_STRING: HSSFRichTextString str = cell.getRichStringCellValue(); if (str != null && str.length() > 0) { value = (str.toString()); } else { value = ExcelToHtmlUtils.EMPTY; } break; case HSSFCell.CELL_TYPE_NUMERIC: HSSFCellStyle style = cellStyle; if (style == null) { value = String.valueOf(cell.getNumericCellValue()); } else { value = (_formatter.formatRawCellContents(cell.getNumericCellValue(), style.getDataFormat(), style.getDataFormatString())); } break; case HSSFCell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: value = ErrorEval.getText(cell.getErrorCellValue()); break; default: logger.log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.getCachedFormulaResultType() + ")"); value = ExcelToHtmlUtils.EMPTY; break; } break; case HSSFCell.CELL_TYPE_BLANK: value = ExcelToHtmlUtils.EMPTY; break; case HSSFCell.CELL_TYPE_NUMERIC: value = _formatter.formatCellValue(cell); break; case HSSFCell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: value = ErrorEval.getText(cell.getErrorCellValue()); break; default: logger.log(POILogger.WARN, "Unexpected cell type (" + cell.getCellType() + ")"); return true; } final boolean noText = ExcelToHtmlUtils.isEmpty(value); final boolean wrapInDivs = !noText && isUseDivsToSpan() && !cellStyle.getWrapText(); final short cellStyleIndex = cellStyle.getIndex(); if (cellStyleIndex != 0) { HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook(); String mainCssClass = getStyleClassName(workbook, cellStyle); if (wrapInDivs) { tableCellElement.setAttribute("class", mainCssClass + " " + cssClassContainerCell); } else { tableCellElement.setAttribute("class", mainCssClass); } if (noText) { /* * if cell style is defined (like borders, etc.) but cell text * is empty, add " " to output, so browser won't collapse * and ignore cell */ value = "\u00A0"; } } if (isOutputLeadingSpacesAsNonBreaking() && value.startsWith(" ")) { StringBuilder builder = new StringBuilder(); for (int c = 0; c < value.length(); c++) { if (value.charAt(c) != ' ') break; builder.append('\u00a0'); } if (value.length() != builder.length()) builder.append(value.substring(builder.length())); value = builder.toString(); } Text text = htmlDocumentFacade.createText(value); if (wrapInDivs) { Element outerDiv = htmlDocumentFacade.createBlock(); outerDiv.setAttribute("class", this.cssClassContainerDiv); Element innerDiv = htmlDocumentFacade.createBlock(); StringBuilder innerDivStyle = new StringBuilder(); innerDivStyle.append("position:absolute;min-width:"); innerDivStyle.append(normalWidthPx); innerDivStyle.append("px;"); if (maxSpannedWidthPx != Integer.MAX_VALUE) { innerDivStyle.append("max-width:"); innerDivStyle.append(maxSpannedWidthPx); innerDivStyle.append("px;"); } innerDivStyle.append("overflow:hidden;max-height:"); innerDivStyle.append(normalHeightPt); innerDivStyle.append("pt;white-space:nowrap;"); ExcelToHtmlUtils.appendAlign(innerDivStyle, cellStyle.getAlignment()); htmlDocumentFacade.addStyleClass(outerDiv, cssClassPrefixDiv, innerDivStyle.toString()); innerDiv.appendChild(text); outerDiv.appendChild(innerDiv); tableCellElement.appendChild(outerDiv); } else { tableCellElement.appendChild(text); } return ExcelToHtmlUtils.isEmpty(value) && cellStyleIndex == 0; }
From source file:com.wangzhu.poi.ExcelToHtmlConverter.java
License:Apache License
protected String buildStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle) { StringBuffer style = new StringBuffer(); style.append("white-space:pre-wrap;"); ExcelToHtmlUtils.appendAlign(style, cellStyle.getAlignment()); if (cellStyle.getFillPattern() == 0) { // no fill } else if (cellStyle.getFillPattern() == 1) { final HSSFColor foregroundColor = cellStyle.getFillForegroundColorColor(); if (foregroundColor != null) { style.append("background-color:" + AbstractExcelUtils.getColor(foregroundColor) + ";"); }//from ww w.ja va2 s. c om } else { final HSSFColor backgroundColor = cellStyle.getFillBackgroundColorColor(); if (backgroundColor != null) { style.append("background-color:" + AbstractExcelUtils.getColor(backgroundColor) + ";"); } } this.buildStyle_border(workbook, style, "top", cellStyle.getBorderTop(), cellStyle.getTopBorderColor()); this.buildStyle_border(workbook, style, "right", cellStyle.getBorderRight(), cellStyle.getRightBorderColor()); this.buildStyle_border(workbook, style, "bottom", cellStyle.getBorderBottom(), cellStyle.getBottomBorderColor()); this.buildStyle_border(workbook, style, "left", cellStyle.getBorderLeft(), cellStyle.getLeftBorderColor()); HSSFFont font = cellStyle.getFont(workbook); this.buildStyle_font(workbook, style, font); return style.toString(); }