/** * 摘要: @说明:Excel样式 @创建:作者:yxy 创建时间:2011-8-17 * * @param workBook * @param cellStyle 样式模型 * @return @修改历史: [序号](yxy 2011-8-17)<修改说明> */ public static HSSFCellStyle getNewStyle(HSSFWorkbook workBook, CellStyle cellStyle) { HSSFCellStyle style = workBook.createCellStyle(); // 对齐方式 style.setAlignment(cellStyle.getAlignment()); style.setVerticalAlignment(cellStyle.getVAlignment()); // 设置背景颜色 // 最好的设置Pattern style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 单元格背景的显示模式 style.setFillForegroundColor(cellStyle.getColor()); // 单元格背景的显示模式. // 设置边框 style.setBorderBottom(cellStyle.getBorderBottom()); // 下边框 style.setBorderLeft(cellStyle.getBorderLeft()); // 左边框 style.setBorderTop(cellStyle.getBorderTop()); // 上边框 style.setBorderRight(cellStyle.getBorderRight()); // 右边框 // 设置边框颜色 style.setBottomBorderColor(cellStyle.getBottomBorderColor()); style.setTopBorderColor(cellStyle.getTopBorderColor()); style.setLeftBorderColor(cellStyle.getLeftBorderColor()); style.setRightBorderColor(cellStyle.getRightBorderColor()); // 设置自动换行 style.setWrapText(cellStyle.getWrapText()); style.setHidden(cellStyle.getHidden()); // 数据格式 style.setDataFormat(cellStyle.getDataFormate()); style.setLocked(cellStyle.getLocked()); // 文本旋转 请注意,这里的Rotation取值是从-90到90,而不是0-180度 style.setRotation(cellStyle.getRotation()); // 文本缩进 style.setIndention(cellStyle.getIndention()); return style; }
public static HSSFCellStyle getNewStyles( HSSFWorkbook workBook, CellStyle cellStyle, HSSFFont font) { HSSFCellStyle style = workBook.createCellStyle(); // 对齐方式 style.setAlignment(cellStyle.getAlignment()); style.setVerticalAlignment(cellStyle.getVAlignment()); // 设置自动换行 style.setWrapText(cellStyle.getWrapText()); style.setHidden(cellStyle.getHidden()); // 数据格式 style.setDataFormat(cellStyle.getDataFormate()); style.setLocked(cellStyle.getLocked()); // 文本旋转 请注意,这里的Rotation取值是从-90到90,而不是0-180度 style.setRotation(cellStyle.getRotation()); // 文本缩进 style.setIndention(cellStyle.getIndention()); // 设置字体 style.setFont(font); return style; }
/** * Compute width of a single cell * * @param cell the cell whose width is to be calculated * @param defaultCharWidth the width of a single character * @param formatter formatter used to prepare the text to be measured * @param useMergedCells whether to use merged cells * @return the width in pixels */ public static double getCellWidth( Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) { Sheet sheet = cell.getSheet(); Workbook wb = sheet.getWorkbook(); Row row = cell.getRow(); int column = cell.getColumnIndex(); int colspan = 1; for (int i = 0; i < sheet.getNumMergedRegions(); i++) { CellRangeAddress region = sheet.getMergedRegion(i); if (containsCell(region, row.getRowNum(), column)) { if (!useMergedCells) { // If we're not using merged cells, skip this one and move on to the next. return -1; } cell = row.getCell(region.getFirstColumn()); colspan = 1 + region.getLastColumn() - region.getFirstColumn(); } } CellStyle style = cell.getCellStyle(); int cellType = cell.getCellType(); // for formula cells we compute the cell width for the cached formula result if (cellType == Cell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType(); Font font = wb.getFontAt(style.getFontIndex()); AttributedString str; TextLayout layout; double width = -1; if (cellType == Cell.CELL_TYPE_STRING) { RichTextString rt = cell.getRichStringCellValue(); String[] lines = rt.getString().split("\\n"); for (int i = 0; i < lines.length; i++) { String txt = lines[i] + defaultChar; str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length()); if (rt.numFormattingRuns() > 0) { // TODO: support rich text fragments } layout = new TextLayout(str.getIterator(), fontRenderContext); if (style.getRotation() != 0) { /* * Transform the text using a scale so that it's height is increased by a multiple of the leading, * and then rotate the text before computing the bounds. The scale results in some whitespace around * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but * is added by the standard Excel autosize. */ AffineTransform trans = new AffineTransform(); trans.concatenate( AffineTransform.getRotateInstance(style.getRotation() * 2.0 * Math.PI / 360.0)); trans.concatenate(AffineTransform.getScaleInstance(1, fontHeightMultiple)); width = Math.max( width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } else { width = Math.max( width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } } } else { String sval = null; if (cellType == Cell.CELL_TYPE_NUMERIC) { // Try to get it formatted to look the same as excel try { sval = formatter.formatCellValue(cell, dummyEvaluator); } catch (Exception e) { sval = String.valueOf(cell.getNumericCellValue()); } } else if (cellType == Cell.CELL_TYPE_BOOLEAN) { sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase(); } if (sval != null) { String txt = sval + defaultChar; str = new AttributedString(txt); copyAttributes(font, str, 0, txt.length()); layout = new TextLayout(str.getIterator(), fontRenderContext); if (style.getRotation() != 0) { /* * Transform the text using a scale so that it's height is increased by a multiple of the leading, * and then rotate the text before computing the bounds. The scale results in some whitespace around * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but * is added by the standard Excel autosize. */ AffineTransform trans = new AffineTransform(); trans.concatenate( AffineTransform.getRotateInstance(style.getRotation() * 2.0 * Math.PI / 360.0)); trans.concatenate(AffineTransform.getScaleInstance(1, fontHeightMultiple)); width = Math.max( width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } else { width = Math.max( width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); } } } return width; }
protected SCellStyle importCellStyle(CellStyle poiCellStyle, boolean inStyleTable) { SCellStyle cellStyle = null; // short idx = poiCellStyle.getIndex(); // ZSS-685 if ((cellStyle = importedStyle.get(poiCellStyle)) == null) { // ZSS-685 cellStyle = book.createCellStyle(inStyleTable); importedStyle.put(poiCellStyle, cellStyle); // ZSS-685 String dataFormat = poiCellStyle.getRawDataFormatString(); if (dataFormat == null) { // just in case dataFormat = SCellStyle.FORMAT_GENERAL; } if (!poiCellStyle.isBuiltinDataFormat()) { cellStyle.setDirectDataFormat(dataFormat); } else { cellStyle.setDataFormat(dataFormat); } cellStyle.setWrapText(poiCellStyle.getWrapText()); cellStyle.setLocked(poiCellStyle.getLocked()); cellStyle.setAlignment(PoiEnumConversion.toHorizontalAlignment(poiCellStyle.getAlignment())); cellStyle.setVerticalAlignment( PoiEnumConversion.toVerticalAlignment(poiCellStyle.getVerticalAlignment())); cellStyle.setRotation(poiCellStyle.getRotation()); // ZSS-918 cellStyle.setIndention(poiCellStyle.getIndention()); // ZSS-915 Color fgColor = poiCellStyle.getFillForegroundColorColor(); Color bgColor = poiCellStyle.getFillBackgroundColorColor(); // if (fgColor == null && bgColor != null) { //ZSS-797 // fgColor = bgColor; // } // ZSS-857: SOLID pattern: switch fillColor and backColor cellStyle.setFillPattern(PoiEnumConversion.toFillPattern(poiCellStyle.getFillPattern())); SColor fgSColor = book.createColor(BookHelper.colorToForegroundHTML(workbook, fgColor)); SColor bgSColor = book.createColor(BookHelper.colorToBackgroundHTML(workbook, bgColor)); if (cellStyle.getFillPattern() == FillPattern.SOLID) { SColor tmp = fgSColor; fgSColor = bgSColor; bgSColor = tmp; } cellStyle.setFillColor(fgSColor); cellStyle.setBackColor(bgSColor); // ZSS-780 cellStyle.setBorderLeft(PoiEnumConversion.toBorderType(poiCellStyle.getBorderLeft())); cellStyle.setBorderTop(PoiEnumConversion.toBorderType(poiCellStyle.getBorderTop())); cellStyle.setBorderRight(PoiEnumConversion.toBorderType(poiCellStyle.getBorderRight())); cellStyle.setBorderBottom(PoiEnumConversion.toBorderType(poiCellStyle.getBorderBottom())); cellStyle.setBorderLeftColor( book.createColor( BookHelper.colorToBorderHTML(workbook, poiCellStyle.getLeftBorderColorColor()))); cellStyle.setBorderTopColor( book.createColor( BookHelper.colorToBorderHTML(workbook, poiCellStyle.getTopBorderColorColor()))); cellStyle.setBorderRightColor( book.createColor( BookHelper.colorToBorderHTML(workbook, poiCellStyle.getRightBorderColorColor()))); cellStyle.setBorderBottomColor( book.createColor( BookHelper.colorToBorderHTML(workbook, poiCellStyle.getBottomBorderColorColor()))); cellStyle.setHidden(poiCellStyle.getHidden()); // same style always use same font cellStyle.setFont(importFont(poiCellStyle)); } return cellStyle; }