예제 #1
0
  /**
   * 摘要: @说明: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;
  }
예제 #2
0
    public void applyTo(HSSFCellStyle style, HSSFWorkbook workbook) {

      style.setAlignment(this.alignment);
      style.setBorderBottom(this.borderBottom);
      style.setBorderLeft(this.borderLeft);
      style.setBorderRight(this.borderRight);
      style.setBorderTop(this.borderTop);
      style.setBottomBorderColor(this.bottomBorderColor);
      style.setDataFormat(this.dataFormat);

      style.setFillPattern(this.fillPattern);
      style.setFillForegroundColor(this.fillForegroundColor);
      style.setFillBackgroundColor(this.fillBackgroundColor);

      style.setFont(workbook.getFontAt(this.fontIndex));

      style.setHidden(this.hidden);
      style.setIndention(this.indention);
      style.setLeftBorderColor(this.leftBorderColor);
      style.setLocked(this.locked);
      style.setRightBorderColor(this.rightBorderColor);
      style.setRotation(this.rotation);
      style.setTopBorderColor(this.topBorderColor);
      style.setVerticalAlignment(this.verticalAlignment);
      style.setWrapText(this.wrapText);
    }
예제 #3
0
  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;
  }
예제 #4
0
 /**
  * Создает новый стиль в документе Excel который полностью копирует свойства некоторого исходного
  * стиля этого же документа. Данный метод используется для того чтобы в последствии поменять в
  * новом стиле один или несколько свойств не затрагивая при этом свойства исходного стиля.
  *
  * @param wb документ Excel.
  * @param src стиль ячейки взятый в качестве шаблона.
  * @return созданная копия исходного стиля в этом же документе.
  */
 public static HSSFCellStyle copyStyle(final HSSFWorkbook wb, final HSSFCellStyle src) {
   final HSSFCellStyle dst = wb.createCellStyle();
   dst.setAlignment(src.getAlignment());
   dst.setBorderBottom(src.getBorderBottom());
   dst.setBorderLeft(src.getBorderLeft());
   dst.setBorderRight(src.getBorderRight());
   dst.setBorderTop(src.getBorderTop());
   dst.setBottomBorderColor(src.getBottomBorderColor());
   dst.setDataFormat(src.getDataFormat());
   dst.setFillForegroundColor(src.getFillForegroundColor());
   dst.setFillBackgroundColor(src.getFillBackgroundColor());
   dst.setFillPattern(src.getFillPattern());
   dst.setFont(src.getFont(wb));
   dst.setHidden(src.getHidden());
   dst.setIndention(src.getIndention());
   dst.setLeftBorderColor(src.getLeftBorderColor());
   dst.setLocked(src.getLocked());
   dst.setRightBorderColor(src.getRightBorderColor());
   dst.setRotation(src.getRotation());
   dst.setTopBorderColor(src.getTopBorderColor());
   dst.setVerticalAlignment(src.getVerticalAlignment());
   dst.setWrapText(src.getWrapText());
   return dst;
 }
  protected Map<Short, HSSFCellStyle> applyStyles(final Report report, final HSSFWorkbook wb) {
    final StylePalette palette = report.getPalette();
    final Map<Short, HSSFCellStyle> styles = new HashMap<Short, HSSFCellStyle>();

    if (report.getTemplate() != null) {
      for (final short styleIndex : palette.getStyles().keySet()) {
        final HSSFCellStyle style = wb.getCellStyleAt(styleIndex);
        if (style == null)
          throw new RuntimeException(
              "Inconsistent report template. Style not found: " + styleIndex);
        styles.put(styleIndex, style);
      }
      return styles;
    }
    if (palette.getColors().size() > PaletteRecord.STANDARD_PALETTE_SIZE)
      throw new RuntimeException("too many colors on report");
    final HSSFPalette pal = wb.getCustomPalette();
    for (final Color color : palette.getColors().values()) {
      pal.setColorAtIndex(color.getId(), color.getRed(), color.getGreen(), color.getBlue());
    }

    final Map<Short, HSSFFont> fonts = new HashMap<Short, HSSFFont>();
    final HSSFDataFormat formatter = wb.createDataFormat();
    for (final Font font : palette.getFonts().values()) {
      final HSSFFont f = POIUtils.ensureFontExists(wb, font);
      fonts.put(font.getId(), f);
    }

    for (final CellStyle style : palette.getStyles().values()) {
      final short bbc =
          style.getBottomBorderColor() != null ? style.getBottomBorderColor().getId() : 0;
      final short fbc =
          style.getFillBackgroundColor() != null ? style.getFillBackgroundColor().getId() : 0;
      final short ffc =
          style.getFillForegroundColor() != null ? style.getFillForegroundColor().getId() : 0;
      final short lbc = style.getLeftBorderColor() != null ? style.getLeftBorderColor().getId() : 0;
      final short rbc =
          style.getRightBorderColor() != null ? style.getRightBorderColor().getId() : 0;
      final short tbc = style.getTopBorderColor() != null ? style.getTopBorderColor().getId() : 0;

      final HSSFCellStyle s = wb.createCellStyle();
      s.setAlignment(style.getAlignment());
      s.setBorderBottom(style.getBorderBottom());
      s.setBorderLeft(style.getBorderLeft());
      s.setBorderRight(style.getBorderRight());
      s.setBorderTop(style.getBorderTop());
      s.setBottomBorderColor(bbc);
      s.setDataFormat(formatter.getFormat(style.getDataFormat()));
      s.setFillBackgroundColor(fbc);
      s.setFillForegroundColor(ffc);
      s.setFillPattern(style.getFillPattern());
      s.setHidden(style.isHidden());
      s.setIndention(style.getIndention());
      s.setLeftBorderColor(lbc);
      s.setLocked(style.isLocked());
      s.setRightBorderColor(rbc);
      s.setRotation(style.getRotation());
      s.setTopBorderColor(tbc);
      s.setVerticalAlignment(style.getVerticalAlignment());
      s.setWrapText(style.isWrapText());
      s.setFont(fonts.get(style.getFont().getId()));
      styles.put(style.getId(), s);
    }
    return styles;
  }