Apache POI条件格式搜索字符串以某些文本开头

时间:2018-08-24 09:19:29

标签: java apache-poi conditional-formatting

如何创建条件格式规则以搜索以文本开头的指定范围内的所有内容?

我可以创建一个规则来搜索指定范围内等于某些文本的所有内容。

 XSSFSheet sheet1 = workbook.getSheet("sheet1");
 XSSFSheetConditionalFormatting sheet1cf = sheet1.getSheetConditionalFormatting();
 XSSFConditionalFormattingRule aRule = sheet1cf.createConditionalFormattingRule(ComparisonOperator.EQUAL,"\"a\"");
//that search value="a"

但是,我不知道如何创建规则来搜索以文本开头的指定范围内的所有内容?

我正在使用Excel 2007。 我正在按照以下步骤创建规则。

  1. 点击条件格式下拉列表
  2. 选择“新规则”
  3. 在“选择规则类型”中选择“仅格式化包含的单元格”
  4. 在“仅设置单元格格式”中,在第一个下拉框中选择“特定文本”
  5. 在第二个下拉框中选择“包含”
  6. 在我的示例中,在第三个下拉框中输入“ a”

最后,为包含“ a”的单元格设置背景色。

1 个答案:

答案 0 :(得分:1)

所以这个问题有两个不同的答案。

首先:您只想使用XSSF并与Excel完全相同即可。

然后我们需要使用apache poi的底层底层对象,因为尽管有apache poiBEGINS_WITH不支持ComparisonOperator STConditionalFormattingOperator.BEGINS_WITH

因此,我们首先需要创建一个具有任何伪ComparisonOperator和任何伪公式的条件格式设置规则。然后我们可以用STConditionalFormattingOperator.BEGINS_WITH和适当的公式替换这些虚拟对象。

示例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

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

import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;

import java.lang.reflect.Field;

import java.io.FileOutputStream;

public class XSSFConditionalFormattingBeginsWith {

 static XSSFConditionalFormattingRule createConditionalFormattingRuleBeginsWith(
         XSSFSheetConditionalFormatting sheetCF, 
         String text) throws Exception {

  XSSFConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(
   ComparisonOperator.EQUAL /*only dummy*/, 
   "" /*only dummy*/);

  Field _cfRule = XSSFConditionalFormattingRule.class.getDeclaredField("_cfRule");
  _cfRule.setAccessible(true); 
  CTCfRule ctCfRule  = (CTCfRule)_cfRule.get(rule); 
  ctCfRule.setType(STCfType.BEGINS_WITH);
  ctCfRule.setOperator(STConditionalFormattingOperator.BEGINS_WITH);
  ctCfRule.setText(text);
  ctCfRule.addFormula("(LEFT(INDEX($1:$1048576, ROW(), COLUMN())," + text.length() + ")=\""+ text + "\")");
  _cfRule.set(rule, ctCfRule);

  return rule;
 }

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

  XSSFSheet sheet = workbook.createSheet("new sheet");

  XSSFSheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
  XSSFConditionalFormattingRule rule = createConditionalFormattingRuleBeginsWith(sheetCF, "bla");

  PatternFormatting fill = rule.createPatternFormatting();
  fill.setFillBackgroundColor(IndexedColors.YELLOW.index);
  fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

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

  CellRangeAddress[] regions = new CellRangeAddress[]{CellRangeAddress.valueOf("A1:B1000")};

  sheetCF.addConditionalFormatting(regions, cfRules);

  workbook.write(new FileOutputStream("XSSFConditionalFormattingBeginsWith.xlsx"));
  workbook.close();

 }
}

第二:您想使用apache poi的高级类并同时支持HSSFXSSF

这时,我们只能使用基于公式的条件格式设置规则。 Excel本身也通过在描述的过程的第3步中选择Use a formula to determine which cells to format来支持此操作。

示例:

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 ConditionalFormattingBeginsWith {

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

  Sheet sheet = workbook.createSheet("new sheet");
  SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

  String text = "bla";
  int lastRow = 1000;

  ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule(
   "(LEFT(INDEX($1:$" + lastRow + ",ROW(),COLUMN())," + text.length() + ")=\"" + text + "\")");

  PatternFormatting fill = rule.createPatternFormatting();
  fill.setFillBackgroundColor(IndexedColors.YELLOW.index);
  fill.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

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

  CellRangeAddress[] regions = new CellRangeAddress[]{CellRangeAddress.valueOf("A1:B" + lastRow)};

  sheetCF.addConditionalFormatting(regions, cfRules);

  if (workbook instanceof XSSFWorkbook) {
   workbook.write(new FileOutputStream("ConditionalFormattingBeginsWith.xlsx"));
  } else if (workbook instanceof HSSFWorkbook) {
   workbook.write(new FileOutputStream("ConditionalFormattingBeginsWith.xls"));
  }
  workbook.close();

 }
}