【问题标题】:Can Apache POI apply Top 10 conditional formatting?Apache POI 可以应用 Top 10 条件格式吗?
【发布时间】:2021-08-11 13:09:06
【问题描述】:

我已经创建了 excel 文件,例如 createConditionalFormattingRule 使用 defrent 类型,例如单元格值等于另一个:

XSSFConditionalFormattingRule my_rule1 = ContedFormat.createConditionalFormattingRule(ComparisonOperator.EQUAL, "$M$" + (CountRower + 1));

但我的问题是,有一个条件格式用于选择 renege 上的前 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 名而不是色阶

【问题讨论】:

  • 这和 Swing 有什么关系?
  • 这是因为我使用 java swing 类来做到这一点
  • “我使用 java swing 类来做这个” 不相关。如果您可以将相同的代码放在控制台应用程序或 servlet 中,那么 Swing 就不是问题的一部分。
  • 感谢您提供的信息,我将更新标签并删除摇摆以使其更清晰且不会令人困惑...您对我的问题有任何想法吗?因为好像我已经完成了 Google Pages ????????
  • “您对我的问题有任何想法吗?” 我对 Swing 非常了解。几乎没有关于问题的内容。

标签: java apache-poi xssf


【解决方案1】:

在当前的Apache poi 5.0.0 SheetConditionalFormatting 中没有为前 10 名创建ConditionalFormattingRule 的方法。但它有SheetConditionalFormatting.createConditionalFormattingColorScaleRule()。因此,使用基础 org.openxmlformats.schemas.spreadsheetml.x2006.main.* 类创建色阶规则的链接示例已过时。

但前 10 项规则设置比色阶规则设置更复杂。对于色阶规则,所有设置都在工作表的CTConditionalFormatting 中。对于前 10 条规则,需要使用填充图案格式。该模式格式链接到工作簿的样式部分。

所以最好的方法是为前 10 名创建一个 XSSFConditionalFormattingRule,它设置类型 STCfType.TOP_10 和排名。这个ConditionalFormattingRule 已经提供了一种创建模式格式的方法。

很遗憾XSSFConditionalFormattingRule 的构造函数以及获取CTCfRule 的方法都不是公开的。所以需要用到反射。

下面的完整示例提供了XSSFConditionalFormattingRule createConditionalFormattingRuleTop10(XSSFSheetConditionalFormatting sheetCF, int rank) 来创建一个XSSFConditionalFormattingRule 用于给定特殊排名的前 10 名。所有其他内容都与 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();

 }
}

【讨论】:

  • 完美答案,完整解释?
猜你喜欢
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
相关资源
最近更新 更多