我一直在尝试使用Java,Apache poi对特定范围的Excel单元格进行条件格式设置。对于具有TRUE或FALSE值的像元,应根据给定规则将背景设置为特定颜色。 在写入文件时应用基于值的格式设置时,相同的代码适用于数字。 在我通过选择每个单元格并双击刷新每个单元格之前,不会应用更改。有什么方法可以使用Java刷新整个工作表吗?
已尝试将EvaluationAllFormulaCells()函数用作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);
答案 0 :(得分:0)
我怀疑您在Excel
中的“ TRUE”和“ FALSE”单元格值实际上不是布尔值TRUE
和FALSE
,而是字符串单元格值。但是您的规则仅检查布尔单元格值。
以下代码的第一部分(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();
}
}