Java 在ApachePOI中创建CellStyle库

Java 在ApachePOI中创建CellStyle库,java,excel,apache-poi,Java,Excel,Apache Poi,我的系统使用来自Java的ApachePOI生成许多不同的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

我的系统使用来自Java的ApachePOI生成许多不同的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());

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

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

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


样式的延迟初始化也可以很好地工作,允许您避免创建重复的样式,但最好保持样式私有,即
private CellStyle headingCellStyle=null以确保没有任何内容可以更改类外的样式分配,并且不会意外使用空值headerCellStyle

我有一个项目,可以将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;
    }
}
公共类生成器{
私有地图样式;
公共样式生成器(){
styles=新的HashMap();
}
公共单元格样式getStyle(单元格、样式){
XSSFCellStyle细胞型;
if(样式.容器(样式)){
cellStyle=styles.get(style);
}否则{
cellStyle=(XSSFCellStyle)cell.getSheet().getWorkbook().createCellStyle();
applyBackground(样式、单元样式);
ApplyOrders(样式、单元格样式);
applyFont(单元格、样式、单元格样式);
applyHorizontalAlignment(样式、单元样式);
applyverticalAlignment(样式、单元格样式);
applyWidth(单元格、样式);
样式。放置(样式,单元格样式);
}
返回单元格样式;
}
受保护的void applyBackground(样式样式、XSSFCellStyle单元格样式){
if(style.isBackgroundSet()){
setFillPattern(XSSFCellStyle.SOLID_前台);
setFillForegroundColor(新的XSSFColor(style.getProperty(CssColorProperty.BACKGROUND));
}
}
受保护的无效ApplyOrders(样式样式、XSSFCellStyle和cellStyle){
if(style.isBorderWidthSet()){
short width=(short)style.getProperty(CssIntegerProperty.BORDER_width);
Color Color=style.getProperty(CssColorProperty.BORDER\u Color)!=null?样式
.getProperty(CssColorProperty.BORDER_COLOR):COLOR.BLACK;
cellStyle.Bottom(BorderStyle.THIN);
单元格样式。底部(宽度);
cellStyle.setBottomBorderColor(新XSSFColor(颜色));
cellStyle.Top(BorderStyle.THIN);
cellStyle.顶部(宽度);
cellStyle.setTopBorderColor(新XSSFColor(颜色));
cellStyle.Left(BorderStyle.THIN);
单元格样式。左(宽);
setLeftBorderColor(新XSSFColor(颜色));
cellStyle.Right(BorderStyle.THIN);
cellStyle.右(宽度);
cellStyle.setRightBorderColor(新XSSFColor(颜色));
}
}
受保护的void applyFont(单元格、样式样式、XSSFCellStyle单元格样式){
Font Font=createFont(cell.getSheet().getWorkbook(),样式);
cellStyle.setFont(字体);
}
受保护的void apply水平对齐(样式样式、XSSFCellStyle单元格样式){
if(style.isHorizontallyAlignedLeft()){
cellStyle.setAlignment(水平对齐.左);
}else if(style.isHorizontallyAlignedRight()){
cellStyle.setAlignment(HorizontalAlignment.RIGHT);
}else if(style.isHorizontallyAlignedCenter()){
cellStyle.setAlignment(水平对齐.中心);
}
}
受保护的void applyverticalAlignment(样式样式,XSSFCellStyle cellStyle){
if(style.isVerticallyAlignedTop()){
室柱。垂直排列(垂直排列。顶部);
}else if(style.isVerticallyAlignedBottom()){
室柱。垂直排列(垂直排列。底部);
}else if(style.isVerticallyAlignedMiddle()){
室柱.垂直排列(垂直排列.中心);
}
}
受保护的void applyWidth(单元格、样式){
if(style.getProperty(CssIntegerProperty.WIDTH)>0){
cell.getSheet().setColumnWidth(cell.getColumnIndex(),style.getProperty(CssIntegerProperty.WIDTH)*50);
}
}
公共字体createFont(工作簿、样式){
Font=workbook.createFont();
if(style.isFontNameSet()){
font.setFontName(style.getProperty(CssStringProperty.font_FAMILY));
}
if(style.isFontSizeSet()){
font.setFontHeightInPoints((短)style.getProperty(CssIntegerProperty.font_SIZE));
}
if(style.isColorSet()){
Color Color=style.getProperty(CssColorProperty.Color);
//如果(!color.equals(color.WHITE))//POI错误
// {
((XSSFFont)font).setColor(新XSSFColor(color));
// }
}
if(style.isFontBold()){
font.setBoldweight(font.BOLDWEIGHT\u BOLD);
}
font.setItalic(style.isFontItalic());
if(style.isTextUnderlined()){
font.setUnderline(font.U_SINGLE);
}
返回字体;
}
}
谢谢。这