Установите для обоих :focusable
и :focusableInTouchMode
значение true и вызовите requestFocus
. Это трюк.
Я подозреваю, что ваши значения «TRUE» и «FALSE» в Excel
на самом деле являются не значениями логических ячеек 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();
}
}