我已经创建了已经有的excel文件,例如createConditionalFormattingRule,如果单元格的值等于另一个单元格的值,则使用defrent种类:
XSSFConditionalFormattingRule my_rule1 = ContedFormat.createConditionalFormattingRule(ComparisonOperator.EQUAL, "$M$" + (CountRower + 1));但我的问题是,有一种条件格式用于选择违约的前10个值。
Apache POI可以创建这种类型吗?
编辑:我找到了类似这样的东西:
CTConditionalFormatting TopScall =
sheet.getCTWorksheet().addNewConditionalFormatting();
TopScall.setSqref(my_range);
CTCfRule myCFRule = TopScall.addNewCfRule();
myCFRule.setType(STCfType.TOP_10);
myCFRule.setPriority(1);我试着添加了Formily卷,但没有格式化,值为0
这是关于Conditional Formatting Color Scale的例子,我想做的是类似的事情,但我需要前10名而不是颜色尺度
发布于 2021-08-12 11:05:28
在当前的Apache poi 5.0.0中,SheetConditionalFormatting没有为前10名创建ConditionalFormattingRule的方法,但它有SheetConditionalFormatting.createConditionalFormattingColorScaleRule()。因此,使用底层org.openxmlformats.schemas.spreadsheetml.x2006.main.*类创建色标规则的链接excample已过时。
但前10个规则设置比色阶规则设置更复杂。对于色标规则,所有设置都在工作表的CTConditionalFormatting中。对于前10条规则,需要使用填充样式格式。该模式格式链接到工作簿的样式部分。
因此,最好的方法是为前10名创建一个设置类型STCfType.TOP_10和排名的XSSFConditionalFormattingRule。此ConditionalFormattingRule已经提供了一种创建模式格式的方法。
不幸的是,XSSFConditionalFormattingRule的构造函数以及获取CTCfRule的方法都不是公共的。所以需要使用反射。
下面的完整示例提供了XSSFConditionalFormattingRule createConditionalFormattingRuleTop10(XSSFSheetConditionalFormatting sheetCF, int rank)来为给定特殊排名的前10名创建XSSFConditionalFormattingRule。所有其他内容都类似于Busy Developers' Guide to HSSF and XSSF Features - Conditional Formatting中描述的创建条件格式的默认内容。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
import java.io.FileOutputStream;
public class CreateXSSFConditionalFormattingTop10 {
static XSSFConditionalFormattingRule createConditionalFormattingRuleTop10(XSSFSheetConditionalFormatting sheetCF, int rank) throws Exception {
Field _sheet = XSSFSheetConditionalFormatting.class.getDeclaredField("_sheet");
_sheet.setAccessible(true);
XSSFSheet sheet = (XSSFSheet)_sheet.get(sheetCF);
Constructor constructor = XSSFConditionalFormattingRule.class.getDeclaredConstructor(XSSFSheet.class);
constructor.setAccessible(true);
XSSFConditionalFormattingRule rule = (XSSFConditionalFormattingRule)constructor.newInstance(sheet);
Field _cfRule = XSSFConditionalFormattingRule.class.getDeclaredField("_cfRule");
_cfRule.setAccessible(true);
CTCfRule cfRule = (CTCfRule)_cfRule.get(rule);
cfRule.setType(STCfType.TOP_10);
cfRule.setRank(rank);
return rule;
}
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook(); String filePath = "./CreateXSSFConditionalFormattingTop10.xlsx";
Sheet sheet = workbook.createSheet();
java.util.Random random = new java.util.Random();
for (int r = 0; r < 100; r++) {
sheet.createRow(r).createCell(0).setCellValue(random.nextInt(100)+r);
}
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
if (sheetCF instanceof XSSFSheetConditionalFormatting) {
XSSFConditionalFormattingRule rule = createConditionalFormattingRuleTop10((XSSFSheetConditionalFormatting)sheetCF, 10);
XSSFPatternFormatting fill = rule.createPatternFormatting();
fill.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
XSSFConditionalFormattingRule[] cfRules = new XSSFConditionalFormattingRule[]{rule};
CellRangeAddress[] regions = new CellRangeAddress[]{CellRangeAddress.valueOf("A1:A100")};
sheetCF.addConditionalFormatting(regions, cfRules);
}
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}https://stackoverflow.com/questions/68742674
复制相似问题