在Apache POI中创建CellStyle库

时间:2014-05-11 16:01:32

标签: java excel apache-poi

我的系统使用Java中的Apache POI生成许多不同的Excel报告。

很多这些报告都有相同的风格。

我创建了一个CellStyle库供所有报告使用。我想知道是否有更简洁的方式。

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

public class CellStyles {
    CellStyle headingCellStyle = null;
    Workbook wb;

    public CellStyles(Workbook wb) {
        this.wb = wb;
    }

    public CellStyle getHeadingCellStyle() {
        if (headingCellStyle == null) {
            headingCellStyle = wb.createCellStyle();
            headingCellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
            headingCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        }
        return headingCellStyle;
    }

}

然后调用它

Workbook wb = new XSSFWorkbook(inputStream); // XSSF for .xlsm
CellStyles cs = new CellStyles(wb);


CellUtil.getCell(myRow, 2).setCellStyle(cs.getHeadingCellStyle());

2 个答案:

答案 0 :(得分:7)

我认为,考虑到它的简单性,这个解决方案很好。

不幸的是,POI CellStyle需要从Workbook创建,因此您无法以某种方式避免将wb作为参数传递。

除了您的实现之外,您可能会尝试在CellStyles中以static方式公开wb作为参数并返回样式,这样您就不需要在代码中传递cs对象。虽然我不认为正确地执行它是值得的,但您需要维护Map[Workbook, CellStyles]映射的静态缓存,这将用于返回样式。

样式的延迟初始化效果也很好,并且允许您避免创建重复的样式,但最好将样式保持为私有,即private CellStyle headingCellStyle = null;以确保没有任何内容可以更改类外的样式赋值和null值headerCellStyle不会被意外使用。

答案 1 :(得分:5)

我有一个项目可以将HTML / CSS转换为各种格式,包括Excel和ODF。如果它有任何用处,我会执行以下操作:Style是一个包含从CSS中提取的各种属性的类。

public class ExcelStyleGenerator {
    private Map<Style, XSSFCellStyle> styles;

    public ExcelStyleGenerator() {
        styles = new HashMap<Style, XSSFCellStyle>();
    }

    public CellStyle getStyle(Cell cell, Style style) {
        XSSFCellStyle cellStyle;

        if (styles.containsKey(style)) {
            cellStyle = styles.get(style);
        } else {
            cellStyle = (XSSFCellStyle) cell.getSheet().getWorkbook().createCellStyle();

            applyBackground(style, cellStyle);
            applyBorders(style, cellStyle);
            applyFont(cell, style, cellStyle);
            applyHorizontalAlignment(style, cellStyle);
            applyverticalAlignment(style, cellStyle);
            applyWidth(cell, style);

            styles.put(style, cellStyle);
        }

        return cellStyle;
    }

    protected void applyBackground(Style style, XSSFCellStyle cellStyle) {
        if (style.isBackgroundSet()) {
            cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor(new XSSFColor(style.getProperty(CssColorProperty.BACKGROUND)));
        }
    }

    protected void applyBorders(Style style, XSSFCellStyle cellStyle) {
        if (style.isBorderWidthSet()) {
            short width = (short) style.getProperty(CssIntegerProperty.BORDER_WIDTH);

            Color color = style.getProperty(CssColorProperty.BORDER_COLOR) != null ? style
                    .getProperty(CssColorProperty.BORDER_COLOR) : Color.BLACK;

            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderBottom(width);
            cellStyle.setBottomBorderColor(new XSSFColor(color));

            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderTop(width);
            cellStyle.setTopBorderColor(new XSSFColor(color));

            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderLeft(width);
            cellStyle.setLeftBorderColor(new XSSFColor(color));

            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderRight(width);
            cellStyle.setRightBorderColor(new XSSFColor(color));
        }
    }

    protected void applyFont(Cell cell, Style style, XSSFCellStyle cellStyle) {
        Font font = createFont(cell.getSheet().getWorkbook(), style);
        cellStyle.setFont(font);
    }

    protected void applyHorizontalAlignment(Style style, XSSFCellStyle cellStyle) {
        if (style.isHorizontallyAlignedLeft()) {
            cellStyle.setAlignment(HorizontalAlignment.LEFT);
        } else if (style.isHorizontallyAlignedRight()) {
            cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        } else if (style.isHorizontallyAlignedCenter()) {
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
        }
    }

    protected void applyverticalAlignment(Style style, XSSFCellStyle cellStyle) {
        if (style.isVerticallyAlignedTop()) {
            cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
        } else if (style.isVerticallyAlignedBottom()) {
            cellStyle.setVerticalAlignment(VerticalAlignment.BOTTOM);
        } else if (style.isVerticallyAlignedMiddle()) {
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        }
    }

    protected void applyWidth(Cell cell, Style style) {
        if (style.getProperty(CssIntegerProperty.WIDTH) > 0) {
            cell.getSheet().setColumnWidth(cell.getColumnIndex(), style.getProperty(CssIntegerProperty.WIDTH) * 50);
        }
    }

    public Font createFont(Workbook workbook, Style style) {
        Font font = workbook.createFont();

        if (style.isFontNameSet()) {
            font.setFontName(style.getProperty(CssStringProperty.FONT_FAMILY));
        }

        if (style.isFontSizeSet()) {
            font.setFontHeightInPoints((short) style.getProperty(CssIntegerProperty.FONT_SIZE));
        }

        if (style.isColorSet()) {
            Color color = style.getProperty(CssColorProperty.COLOR);

            // if(! color.equals(Color.WHITE)) // POI Bug
            // {
            ((XSSFFont) font).setColor(new XSSFColor(color));
            // }
        }

        if (style.isFontBold()) {
            font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        }

        font.setItalic(style.isFontItalic());

        if (style.isTextUnderlined()) {
            font.setUnderline(Font.U_SINGLE);
        }

        return font;
    }
}