コード例 #1
0
 private void setVerticalAlignment(Tc tableCell, Map<String, Object> style) {
   if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) {
     if (StyleFormatConstants.VERTICAL_ALIGN_TOP.equals(
         style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
       setVerticalAlignment(tableCell, STVerticalJc.TOP);
     }
     if (StyleFormatConstants.VERTICAL_ALIGN_MIDDLE.equals(
         style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
       setVerticalAlignment(tableCell, STVerticalJc.CENTER);
     }
     if (StyleFormatConstants.VERTICAL_ALIGN_BOTTOM.equals(
         style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
       setVerticalAlignment(tableCell, STVerticalJc.BOTTOM);
     }
   }
 }
コード例 #2
0
  private XSSFCellStyle buildBandElementStyle(
      BandElement bandElement, Object value, int gridRow, int gridColumn, int colSpan) {
    Map<String, Object> style = buildCellStyleMap(bandElement, value, gridRow, gridColumn, colSpan);
    XSSFCellStyle cellStyle = null;
    XSSFFont cellFont = null;
    int fontKey = -1;
    int styleKey = -1;
    // we have to create new fonts and styles if some formatting conditions are met
    // also for subreports we may have a subreportCellStyle passed by ReportBandElement
    boolean cacheFont = false;
    boolean cacheAllFont = false;
    boolean cacheStyle = false;
    boolean cacheAllStyle = false;
    if ((modifiedStyle[gridRow][gridColumn]) || bean.isSubreport()) {
      fontKey = getFontKey(style);
      if (fontKey != -1) {
        cellFont = condFonts.get(fontKey);
      }
      if (cellFont == null) {
        cellFont = wb.createFont();
        cacheFont = true;
      }
      styleKey = getStyleKey(style, bandElement);
      if (styleKey != -1) {
        cellStyle = condStyles.get(styleKey);
      }
      if (cellStyle == null) {
        cellStyle = wb.createCellStyle();
        cacheStyle = true;
      }
      modifiedStyle[gridRow][gridColumn] = false;
    } else {
      fontKey = getFontKey(style);
      if (fontKey != -1) {
        cellFont = fonts.get(fontKey);
      }
      if ((cellFont == null) && (bandElement != null)) {
        cellFont = wb.createFont();
        cacheAllFont = true;
      }
      styleKey = getStyleKey(style, bandElement);
      if (styleKey != -1) {
        cellStyle = styles.get(styleKey);
      }
      if (cellStyle == null) {
        cellStyle = wb.createCellStyle();
        cacheAllStyle = true;
      }
    }

    // HSSFPalette cellPal = wb.getCustomPalette();
    if (style.containsKey(StyleFormatConstants.FONT_FAMILY_KEY)) {
      String val = (String) style.get(StyleFormatConstants.FONT_FAMILY_KEY);
      cellFont.setFontName(val);
    }
    if (style.containsKey(StyleFormatConstants.FONT_SIZE)) {
      Float val = (Float) style.get(StyleFormatConstants.FONT_SIZE);
      cellFont.setFontHeightInPoints(val.shortValue());
    }
    if (style.containsKey(StyleFormatConstants.FONT_COLOR)) {
      Color val = (Color) style.get(StyleFormatConstants.FONT_COLOR);
      cellFont.setColor(ExcelColorSupport.getNearestColor(val));
    }
    if (style.containsKey(StyleFormatConstants.FONT_STYLE_KEY)) {
      if (StyleFormatConstants.FONT_STYLE_NORMAL.equals(
          style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
        cellFont.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
      }
      if (StyleFormatConstants.FONT_STYLE_BOLD.equals(
          style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
        cellFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
      }
      if (StyleFormatConstants.FONT_STYLE_ITALIC.equals(
          style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
        cellFont.setItalic(true);
      }
      if (StyleFormatConstants.FONT_STYLE_BOLDITALIC.equals(
          style.get(StyleFormatConstants.FONT_STYLE_KEY))) {
        cellFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        cellFont.setItalic(true);
      }
    }

    if (cacheFont && (fontKey != -1)) {
      condFonts.put(fontKey, cellFont);
    }
    if (cacheAllFont && (fontKey != -1)) {
      fonts.put(fontKey, cellFont);
    }
    if (style.containsKey(StyleFormatConstants.BACKGROUND_COLOR)) {
      Color val = (Color) style.get(StyleFormatConstants.BACKGROUND_COLOR);
      cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
      cellStyle.setFillForegroundColor(ExcelColorSupport.getNearestColor(val));
    }
    if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) {
      if (StyleFormatConstants.HORIZONTAL_ALIGN_LEFT.equals(
          style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
        cellStyle.setAlignment((short) 1);
      }
      if (StyleFormatConstants.HORIZONTAL_ALIGN_RIGHT.equals(
          style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
        cellStyle.setAlignment((short) 3);
      }
      if (StyleFormatConstants.HORIZONTAL_ALIGN_CENTER.equals(
          style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
        cellStyle.setAlignment((short) 2);
      }
    }

    if (style.containsKey(StyleFormatConstants.VERTICAL_ALIGN_KEY)) {
      if (StyleFormatConstants.VERTICAL_ALIGN_TOP.equals(
          style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
      }
      if (StyleFormatConstants.VERTICAL_ALIGN_MIDDLE.equals(
          style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
      }
      if (StyleFormatConstants.VERTICAL_ALIGN_BOTTOM.equals(
          style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) {
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
      }
    } else {
      cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    }

    short left = 0, right = 0, top = 0, bottom = 0;
    Color leftColor = Color.BLACK,
        rightColor = Color.BLACK,
        topColor = Color.BLACK,
        bottomColor = Color.BLACK;
    if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) {
      Float val = (Float) style.get(StyleFormatConstants.BORDER_LEFT);
      //
      left = val.shortValue();
      if (left == BORDER_THIN_VALUE) {
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
      }
      if (left == BORDER_MEDIUM_VALUE) {
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_MEDIUM);
      }
      if (left == BORDER_THICK_VALUE) {
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THICK);
      }

      Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR);
      leftColor = color;
      cellStyle.setLeftBorderColor(ExcelColorSupport.getNearestColor(color));
    }
    if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) {
      Float val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT);
      //
      right = val.shortValue();
      if (right == BORDER_THIN_VALUE) {
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
      }
      if (right == BORDER_MEDIUM_VALUE) {
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_MEDIUM);
      }
      if (right == BORDER_THICK_VALUE) {
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THICK);
      }
      Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR);
      rightColor = color;
      cellStyle.setRightBorderColor(ExcelColorSupport.getNearestColor(color));
    }
    if (style.containsKey(StyleFormatConstants.BORDER_TOP)) {
      Float val = (Float) style.get(StyleFormatConstants.BORDER_TOP);
      //
      top = val.shortValue();
      if (top == BORDER_THIN_VALUE) {
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
      }
      if (top == BORDER_MEDIUM_VALUE) {
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_MEDIUM);
      }
      if (top == BORDER_THICK_VALUE) {
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THICK);
      }
      Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR);
      topColor = color;
      cellStyle.setTopBorderColor(ExcelColorSupport.getNearestColor(color));
    }
    if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) {
      Float val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM);
      //
      bottom = val.shortValue();
      if (bottom == BORDER_THIN_VALUE) {
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
      }
      if (bottom == BORDER_MEDIUM_VALUE) {
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
      }
      if (bottom == BORDER_THICK_VALUE) {
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THICK);
      }
      Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR);
      bottomColor = color;
      cellStyle.setBottomBorderColor(ExcelColorSupport.getNearestColor(color));
    }
    border = new Border(left, right, top, bottom);
    border.setLeftColor(leftColor);
    border.setRightColor(rightColor);
    border.setTopColor(topColor);
    border.setBottomColor(bottomColor);

    if (cellFont != null) {
      cellStyle.setFont(cellFont);
    }

    if (style.containsKey(StyleFormatConstants.PATTERN)) {
      String pattern = (String) style.get(StyleFormatConstants.PATTERN);
      XSSFDataFormat format = wb.createDataFormat();
      cellStyle.setDataFormat(format.getFormat(pattern));
    } else {
      cellStyle.setDataFormat((short) 0);
    }

    if (bandElement != null) {
      cellStyle.setWrapText(bandElement.isWrapText());
    }

    cellStyle =
        updateSubreportBandElementStyle(
            cellStyle, bandElement, value, gridRow, gridColumn, colSpan);

    if (cacheStyle && (styleKey != -1)) {
      condStyles.put(styleKey, cellStyle);
    }
    if (cacheAllStyle && (styleKey != -1)) {
      styles.put(styleKey, cellStyle);
    }

    return cellStyle;
  }