簡體   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