Example #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;
  }
  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;
  }