Apache POI 条件格式搜索字符串以某些文本开头
Apache POI conditional formatting search for string starts with some text
如何创建条件格式规则来搜索指定范围内以某些文本开头的所有内容?
我可以创建一个规则来搜索指定范围内等于某些文本的所有内容。
XSSFSheet sheet1 = workbook.getSheet("sheet1");
XSSFSheetConditionalFormatting sheet1cf = sheet1.getSheetConditionalFormatting();
XSSFConditionalFormattingRule aRule = sheet1cf.createConditionalFormattingRule(ComparisonOperator.EQUAL,"\"a\"");
//that search value="a"
但是,我不知道如何创建一个规则来搜索指定范围内以某些文本开头的所有内容?
我正在使用 Excel 2007。
我正在使用以下步骤来创建规则。
- 单击条件格式下拉菜单
- Select "new rule"
- Select "Format only cells that contain" 在 "Select rule type"
- 第一个下拉框中"Format only cells"、select"Specific Text"
- Select "contains" 在第二个下拉框中
- 在我的例子中,在第三个下拉框中输入"a"
最后,为包含 "a" 的单元格设置背景颜色。
所以这个问题有两种不同的答案。
首先:您只想使用 XSSF
,并且根据您的描述与 Excel
完全相同。
然后我们需要使用 apache poi
的低级底层对象,因为 apache poi
不支持 ComparisonOperator BEGINS_WITH
尽管有 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(:48576, 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
的高级 类 并同时支持 HSSF
和 XSSF
。
那么我们只能使用基于公式的条件格式规则。 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(:$" + 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();
}
}
如何创建条件格式规则来搜索指定范围内以某些文本开头的所有内容?
我可以创建一个规则来搜索指定范围内等于某些文本的所有内容。
XSSFSheet sheet1 = workbook.getSheet("sheet1");
XSSFSheetConditionalFormatting sheet1cf = sheet1.getSheetConditionalFormatting();
XSSFConditionalFormattingRule aRule = sheet1cf.createConditionalFormattingRule(ComparisonOperator.EQUAL,"\"a\"");
//that search value="a"
但是,我不知道如何创建一个规则来搜索指定范围内以某些文本开头的所有内容?
我正在使用 Excel 2007。 我正在使用以下步骤来创建规则。
- 单击条件格式下拉菜单
- Select "new rule"
- Select "Format only cells that contain" 在 "Select rule type"
- 第一个下拉框中"Format only cells"、select"Specific Text"
- Select "contains" 在第二个下拉框中
- 在我的例子中,在第三个下拉框中输入"a"
最后,为包含 "a" 的单元格设置背景颜色。
所以这个问题有两种不同的答案。
首先:您只想使用 XSSF
,并且根据您的描述与 Excel
完全相同。
然后我们需要使用 apache poi
的低级底层对象,因为 apache poi
不支持 ComparisonOperator BEGINS_WITH
尽管有 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(:48576, 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
的高级 类 并同时支持 HSSF
和 XSSF
。
那么我们只能使用基于公式的条件格式规则。 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(:$" + 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();
}
}