java中if条件格式_POI⽣成条件格式
⽤EXCEL导出报表时,很多情况下,需要在EXCEL中⽤颜⾊区分各种数据, 当然可以在创建Cell的时候,直接赋值style 来实现,但还有⼀种⽅式,可以通过设置条件格式来实现。只要满⾜设定的规则,就显⽰不同的颜⾊等。
[/code]
下⾯这段代码例⼦就实现了这样⼀个功能。package com.yihaomen.poi.sample;
import java.io.*;
huangshu
import org.apache.poi.ss.urmodel.*;
import org.apache.poi.hssf.urmodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.urmodel.ComparisonOperator;
辞职信英文
import org.apache.poi.ss.urmodel.IndexedColors;
marriage
import org.apache.poi.ss.util.CellRangeAddress;
落实英文public class CondictionRule {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet - Add Input Rows */
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = ateSheet("Conditional_Formatting");
Row row = ateRow(0);
Cell cell = ateCell(0);
cell.tCellValue(new Double(10));
row = ateRow(1);
cell = ateCell(0);
cell.tCellValue(new Double(12));
row = ateRow(2);
cell = ateCell(0);
cell.tCellValue(new Double(13));
row = ateRow(3);
cell = ateCell(0);
cell.tCellValue(new Double(18));
/* Access conditional formatting facet layer */
HSSFSheetConditionalFormatting my_cond_format_layer = SheetConditionalFormatting();
/* Create a Rule - Less than or Equal to */
HSSFConditionalFormattingRule my_rule =
my_cond_ateConditionalFormattingRule(ComparisonOperator.LE, "10");
在线中文/* Define font formatting if rule is met */
HSSFFontFormatting my_rule_pattern = ateFontFormatting();
my_rule_pattern.tFontColorIndex(Index());
/* Set background fill to Gold*/
HSSFPatternFormatting fill_pattern = atePatternFormatting();
fill_pattern.tFillBackgroundColor(IndexedColors.GOLD.index);
/* Define cond conditional formatting rules - multiple conditions- greater than or equal to */
HSSFConditionalFormattingRule my_rule_2 =
my_cond_ateConditionalFormattingRule(ComparisonOperator.GE, "13");
/* Format borders for all matching cells using HSSFBorderFormatting */
HSSFBorderFormatting border_pattern=my_ateBorderFormatting();
border_pattern.tBorderLeft(BorderFormatting.BORDER_DOUBLE);
border_pattern.tBorderRight(BorderFormatting.BORDER_DOUBLE);
border_pattern.tBorderTop(BorderFormatting.BORDER_DOUBLE);
border_pattern.tBorderBottom(BorderFormatting.BORDER_DOUBLE);
/* You can t border color too, by using relevant methods */
/* Set fill color to Green */
HSSFPatternFormatting fill_pattern_2 = my_atePatternFormatting();
fill_pattern_2.tFillBackgroundColor(IndexedColors.YELLOW.index);
/* OK, we have defined mutliple rules. Time to attach two rules to same range. We create an array of rules now */ ConditionalFormattingRule [] multiple_rules = {my_rule,my_rule_2};
/* Create a Cell Range Address */
CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("A1:A4")};
/* Attach array of rules to the same range */
my_cond_format_layer.addConditionalFormatting(my_data_range,multiple_rules);
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("C:\\multiple_conditonal_formatting.xls"));
my_workbook.write(out);
out.clo();
}
}
如果你⽤的是XSSF的话,⽤如下⽅式:
import java.io.*;
import org.apache.poi.ss.urmodel.*;
import org.apache.poi.hssf.urmodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.urmodel.ComparisonOperator;
import org.apache.poi.ss.urmodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
public class MultipleRulesConditionalFormatting {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet - Add Input Rows */
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = ateSheet("Conditional_Formatting");
Row row = ateRow(0);
Cell cell = ateCell(0);
cell.tCellValue(new Double(10));
日冕是什么
row = ateRow(1);
cell = ateCell(0);
cell.tCellValue(new Double(12));
row = ateRow(2);
cell = ateCell(0);
cell.tCellValue(new Double(13));
row = ateRow(3);
cell = ateCell(0);
cell.tCellValue(new Double(18));
/* Access conditional formatting facet layer */
HSSFSheetConditionalFormatting my_cond_format_layer = SheetConditionalFormatting(); /* Create a Rule - Less than or Equal to */
HSSFConditionalFormattingRule my_rule =
中秋节快乐英文怎么说my_cond_ateConditionalFormattingRule(ComparisonOperator.LE, "10");
/* Define font formatting if rule is met */
英文名字女孩
HSSFFontFormatting my_rule_pattern = ateFontFormatting();
my_rule_pattern.tFontColorIndex(Index());
/* Set background fill to Gold*/
HSSFPatternFormatting fill_pattern = atePatternFormatting();receipt
fill_pattern.tFillBackgroundColor(IndexedColors.GOLD.index);
/* Define cond conditional formatting rules - multiple conditions- greater than or equal to */
HSSFConditionalFormattingRule my_rule_2 =
my_cond_ateConditionalFormattingRule(ComparisonOperator.GE, "13");
/* Format borders for all matching cells using HSSFBorderFormatting */
HSSFBorderFormatting border_pattern=my_ateBorderFormatting();
border_pattern.tBorderLeft(BorderFormatting.BORDER_DOUBLE);
border_pattern.tBorderRight(BorderFormatting.BORDER_DOUBLE);
border_pattern.tBorderTop(BorderFormatting.BORDER_DOUBLE);
border_pattern.tBorderBottom(BorderFormatting.BORDER_DOUBLE);
/* You can t border color too, by using relevant methods */
/* Set fill color to Green */
HSSFPatternFormatting fill_pattern_2 = my_atePatternFormatting();
中国研究生招生信息网报名入口fill_pattern_2.tFillBackgroundColor(IndexedColors.YELLOW.index);
/* OK, we have defined mutliple rules. Time to attach two rules to same range. We create an array of rules now */ ConditionalFormattingRule [] multiple_rules = {my_rule,my_rule_2};
/* Create a Cell Range Address */
CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("A1:A4")};
/* Attach array of rules to the same range */
my_cond_format_layer.addConditionalFormatting(my_data_range,multiple_rules);
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("C:\\multiple_conditonal_formatting.xls"));
my_workbook.write(out);
out.clo();
}
}