使用Java应用于Excel工作表的条件格式,即使在刷新后也不会应用于单元格

问题描述 投票:0回答:1

我一直在尝试使用Java,Apache poi为特定范围的Excel单元格进行条件格式化。对于具有TRUE或FALSE值的单元格,应根据给定的规则将背景设置为特定颜色。在编写文件时应用基于值的格式化时,相同的代码适用于数字。在我通过选择它们并双击来刷新每个单元格之前,不会应用更改。有没有办法使用Java刷新整个工作表?

尝试使用evaluateAllFormulaCells()函数作为XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);

但这也没有做出任何改变。条件格式化方法如下:

SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

              ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(CFRuleRecord.ComparisonOperator.EQUAL, "FALSE");
              PatternFormatting fill1 = rule1.createPatternFormatting();
              fill1.setFillBackgroundColor(IndexedColors.RED.index);
              fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);


              ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(CFRuleRecord.ComparisonOperator.EQUAL, "TRUE");
              PatternFormatting fill2 = rule2.createPatternFormatting();
              fill2.setFillBackgroundColor(IndexedColors.GREEN.index);
              fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

           CellRangeAddress[] regions = {
                   CellRangeAddress.valueOf("AP1:BH47")
           };

           sheetCF.addConditionalFormatting(regions, rule1, rule2);
java excel apache-poi conditional-formatting
1个回答
0
投票

我怀疑Excel中的“TRUE”和“FALSE”单元格值实际上不是布尔单元格值TRUEFALSE,而是字符串单元格值。但是您的规则只检查布尔单元格值。

以下代码的第一部分(A1:A4中的条件格式规则)显示了该问题。第二部分(C1:C4中的条件格式规则)显示了如何使用公式条件格式规则来检查两者,布尔值TRUE / FALSE以及字符串“TRUE”/“FALSE”。

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.apache.poi.ss.util.CellRangeAddress;

public class ConditionalFormattingBooleanValues {

 public static void main(String[] args) throws Exception {
  //Workbook workbook = new HSSFWorkbook();
  Workbook workbook = new XSSFWorkbook();

  Sheet sheet = workbook.createSheet();

  sheet.createRow(0).createCell(0).setCellValue(true); //boolean value TRUE in A1
  sheet.getRow(0).createCell(2).setCellValue(true); //boolean value TRUE in C1
  sheet.createRow(1).createCell(0).setCellValue(false); //boolean value FALSE in A2
  sheet.getRow(1).createCell(2).setCellValue(false); //boolean value FALSE in C2
  sheet.createRow(2).createCell(0).setCellValue("TRUE"); //text value "TRUE" in A3
  sheet.getRow(2).createCell(2).setCellValue("TRUE"); //text value "TRUE" in C3
  sheet.createRow(3).createCell(0).setCellValue("FALSE"); //text value "FALSE" in A4
  sheet.getRow(3).createCell(2).setCellValue("FALSE"); //text value "FALSE" in C4

  SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

  ConditionalFormattingRule rule1;
  ConditionalFormattingRule rule2;
  PatternFormatting fill;
  ConditionalFormattingRule[] cfRules;
  CellRangeAddress[] regions;

  // check only boolean values in rules in A1:A4
  rule1 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "FALSE");
  fill = rule1.createPatternFormatting();
  fill.setFillBackgroundColor(IndexedColors.RED.index);
  fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
  rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "TRUE");
  fill = rule2.createPatternFormatting();
  fill.setFillBackgroundColor(IndexedColors.GREEN.index);
  fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
  cfRules = new ConditionalFormattingRule[]{rule1, rule2};
  regions = new CellRangeAddress[]{CellRangeAddress.valueOf("A1:A4")};
  sheetCF.addConditionalFormatting(regions, cfRules);

  // check boolean and text values in rules in C1:C4
  String formula1 = 
   (workbook instanceof HSSFWorkbook)?"OR(INDIRECT(\"C\"&ROW())=FALSE,INDIRECT(\"C\"&ROW())=\"FALSE\")":"OR(C1=FALSE,C1=\"FALSE\")";
  rule1 = sheetCF.createConditionalFormattingRule(formula1);
  fill = rule1.createPatternFormatting();
  fill.setFillBackgroundColor(IndexedColors.RED.index);
  fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
  String formula2 = 
   (workbook instanceof HSSFWorkbook)?"OR(INDIRECT(\"C\"&ROW())=TRUE,INDIRECT(\"C\"&ROW())=\"TRUE\")":"OR(C1=TRUE,C1=\"TRUE\")";
  rule2 = sheetCF.createConditionalFormattingRule(formula2);
  fill = rule2.createPatternFormatting();
  fill.setFillBackgroundColor(IndexedColors.GREEN.index);
  fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
  cfRules = new ConditionalFormattingRule[]{rule1, rule2};
  regions = new CellRangeAddress[]{CellRangeAddress.valueOf("C1:C4")};
  sheetCF.addConditionalFormatting(regions, cfRules);

  String fileout = (workbook instanceof HSSFWorkbook)?"ConditionalFormattingBooleanValues.xls":"ConditionalFormattingBooleanValues.xlsx";
  FileOutputStream out = new FileOutputStream(fileout);
  workbook.write(out);
  out.close();
  workbook.close();

 }
}
© www.soinside.com 2019 - 2024. All rights reserved.