使用POI库复制条件格式

时间:2016-02-23 23:40:51

标签: java excel apache-poi conditional-formatting

使用POI java库将一些条件格式应用于Excel文档时,我遇到了一些问题。我试图突出显示重复的文本值。下面的代码正常工作,除了没有设置规则格式的事实。当我打开excel文档时,我看到已添加规则(POST),但没有为其分配任何格式/颜色。

public void conditionalFormatting() throws Exception {
/* Read Workbook and Identify Color Scale Range */ 

    sheet = wb.getSheetAt(0);
    List<String> my_range = new ArrayList<>();
    my_range.add("F2:F" + (rowIndex + 1));

/* Add Conditional Formatting Rule */
    CTConditionalFormatting colorScale = sheet.getCTWorksheet().addNewConditionalFormatting();
    colorScale.setSqref(my_range); // Attach Range to conditional formatting set
    CTCfRule myCFRule = colorScale.addNewCfRule(); //create a rule
    myCFRule.setType(STCfType.DUPLICATE_VALUES); // set type of rule to Colour Scale
    myCFRule.setPriority(1); // rule priority = 1
}

有谁知道如何在规则中添加格式/颜色?

问候,T。Lecoffre

2 个答案:

答案 0 :(得分:1)

首先 - 除非你知道自己在做什么,否则不要使用低级别的CT课程。理想情况下,即使这样也不要这样做 - 太多你可能会出错/错过!

相反,您应该使用Apache POI用户模型类进行条件格式化。有documentation on the POI website about them,您可以start here in the JavaDocs to read about itlook at this program in the POI examples获取完整的可用选项

formatDuplicates method in the Conditional Formats examples,您可以看到您需要的代码如下:

 SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

 ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("COUNTIF($F$2:$F$11,F2)>1");
 FontFormatting font = rule1.createFontFormatting();
 font.setFontStyle(false, true);
 font.setFontColorIndex(IndexedColors.BLUE.index);

 CellRangeAddress[] regions = {
            CellRangeAddress.valueOf("F2:F11")
 };

 sheetCF.addConditionalFormatting(regions, rule1);

这将突出显示蓝色

中的重复项

如果你想使用色标或类似的(不确定那对复制品有用吗?),那么同样的文件中也有一个例子

答案 1 :(得分:0)

在研究了&#34; xlsx&#34;文件结构我能够找到我想要的东西,以便使用CT类。为了理解POI库,我强烈建议任何人查看&#34; xlsx&#34;压缩并研究xml文件(更具体地说 - xl / worksheets / {sheet_name} .xml)。以下是我提出的代码:

public void conditionalFormatting() throws Exception {
    /* Read Workbook and Identify Color Scale Range */ 

    sheet = wb.getSheetAt(0);
    List<String> my_range = new ArrayList<>();
    my_range.add("F2:F" + (rowIndex + 1));

    /* Add Conditional Formatting Rule */
    CTConditionalFormatting colorScale = sheet.getCTWorksheet().addNewConditionalFormatting();
    colorScale.setSqref(my_range); // Attach Range to conditional formatting set
    CTCfRule myCFRule = colorScale.addNewCfRule(); //create a rule
    myCFRule.setType(STCfType.DUPLICATE_VALUES); // set type of rule to Colour Scale
    myCFRule.setPriority(1); // rule priority = 1

    /* Add a differential formatting record */
    myCFRule.setDxfId(createDXFs(wb, 12))
}

/* Create a new differential formatting record */
private static int createDXFs(XSSFWorkbook wb, int size) {
    CTDxfs dxfs = wb.getStylesSource().getCTStylesheet().getDxfs();

    if(dxfs == null) {
        dxfs=wb.getStylesSource().getCTStylesheet().addNewDxfs();
    }

    dxfs.setCount(dxfs.getCount() + 1); // update the dxfs count variable
    CTDxf dxf=dxfs.addNewDxf();

    /* Set rule font size */
    CTFontSize fontSize=dxf.addNewFont().addNewSz();
    fontSize.setVal(size);

    /* Set rule pattern/background color */
    CTFill fill = dxf.addNewFill();
    CTPatternFill pattern = fill.addNewPatternFill();
    CTColor color = pattern.addNewBgColor();
    color.setRgb(javax.xml.bind.DatatypeConverter.parseHexBinary("FFF8696B"));

    return (int) dxfs.getCount() - 1; // return the dxf index
}

问候,T。Lecoffre