繁体   English   中英

Apache poi ConditionalFormatting 无法正常工作

[英]Apache poi ConditionalFormatting not working properly

我正在使用 apache poi 在我的 java 应用程序中创建 excel。

我的用例是A1中的值为Change it

单元格A10A14中的样式将被更改。

为此,我遵循 poi 提供的 ConditionalFormatting 功能。

但是这种风格只适用于单元格A10而不是直到A14

我错过了什么?

代码:

private void addValidations(Sheet sheet) {
    SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
    ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("=A1=\"Change it\"");
    FontFormatting fontFmt = rule1.createFontFormatting();
    fontFmt.setFontStyle(true, false);
    fontFmt.setFontColorIndex(IndexedColors.YELLOW.index);
    BorderFormatting bordFmt = rule1.createBorderFormatting();
    bordFmt.setBorderBottom(BorderStyle.THIN);
    bordFmt.setBorderTop(BorderStyle.THICK);
    bordFmt.setBorderLeft(BorderStyle.DASHED);
    bordFmt.setBorderRight(BorderStyle.DOTTED);
    ConditionalFormattingRule [] cfRules =
            {
                    rule1
            };
    CellRangeAddress[] regions = {
            CellRangeAddress.valueOf("A10:A14")
    };
    sheetCF.addConditionalFormatting(regions, cfRules);
}

代码sheetCF.createConditionalFormattingRule("=A1=\"Change it\""); 根本无法工作。 HSSF中,它会抛出org.apache.poi.ss.formula.FormulaParseException: The specified formula '=A1="Change it"' starts with an equals sign which is not allowed. . XSSF ,它会创建一个损坏的*.xlsx文件。 前导等号不存储在Excel公式单元格中。 t 仅显示在ExcelGUI中。 所以它必须是sheetCF.createConditionalFormattingRule("A1=\"Change it\""); .

并且公式=A1="Change it"在列字母和行号中都是相对的。 因此应用于单元格A10意味着=A1="Change it" 但应用于单元格A11意味着=A2="Change it" 并应用于单元格A12这意味着=A3="Change it"等等。 因此,如果需要更改A10:A14中的所有字体 colors 如果A1的内容发生变化,则必须使用$修复公式中的引用。 所以它必须是sheetCF.createConditionalFormattingRule("A$1=\"Change it\""); .

完整示例:

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

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

import java.io.FileOutputStream;

public class ConditionalFormatting {

 public static void main(String[] args) throws Exception {
  Workbook workbook = new XSSFWorkbook(); String filePath ="./ConditionalFormatting.xlsx";
  //Workbook workbook = new HSSFWorkbook(); String filePath ="./ConditionalFormatting.xls";

  Sheet sheet = workbook.createSheet();

  for (int r = 9; r < 14; r++) {
   sheet.createRow(r).createCell(0).setCellValue("Text in Cell A" + (r+1));
  }

  SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

  ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule("A$1=\"Change it\"");
  FontFormatting fontFormatting = rule.createFontFormatting();
  fontFormatting.setFontStyle(false, true);
  fontFormatting.setFontColorIndex(IndexedColors.YELLOW.index);

  ConditionalFormattingRule[] cfRules = new ConditionalFormattingRule[]{rule};

  CellRangeAddress[] regions = new CellRangeAddress[]{CellRangeAddress.valueOf("A10:A14")};

  sheetCF.addConditionalFormatting(regions, cfRules);

  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  workbook.close();

 }
}

如果A1中的单元格内容为Change it这会将A10:A14中的字体颜色更改为黄色。

如果可行,请尝试此操作。

CellRangeAddress[] regions = new CellRangeAddress[]{ CellRangeAddress.valueOf("A10:A14") };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM