使用poi对excel条件格式设置字体颜色使用自定义的颜色

在poi中设置条件格式也是使用如下代码

XSSFSheetConditionalFormatting scf = target.getSheetConditionalFormatting(); //获得条件格式对象
		//红色格式
		XSSFConditionalFormattingRule cf_R_rule  = scf.createConditionalFormattingRule(ComparisonOperator.LT, "0", null);//设置条件格式规则
		XSSFFontFormatting cf_R = cf_R_rule.createFontFormatting();//创建字体样式
		cf_R.setFontColorIndex(IndexedColors.RED.index);
		//条件格式应用的单元格范围  
		CellRangeAddress[] regions = {new CellRangeAddress(4, 26, 4, 4),
				new CellRangeAddress(4, 26, 12, 12),
				new CellRangeAddress(4, 26, 23, 23),
				new CellRangeAddress(37, 54, 4, 4),
				new CellRangeAddress(37, 54, 12, 12),
				new CellRangeAddress(37, 54, 23, 23)};
		//XSSFConditionalFormattingRule[] cfRules = {cf_R_rule};  
		scf.addConditionalFormatting(regions, cf_R_rule);

现在需求使用自定义的颜色来设置字体是可以采用

XSSFConditionalFormattingRule cf_W_rule_1  = scf.createConditionalFormattingRule(ComparisonOperator.EQUAL, "0", null);//设置条件格式规则
		XSSFFontFormatting cf_W_1 = cf_W_rule_1.createFontFormatting();//创建字体样式
		XSSFColor xssfColor = new XSSFColor(new java.awt.Color(204,255, 255));
		cf_W_1.setFontColorIndex(xssfColor.getIndex());
		cf_W_1.setFontColor(xssfColor);
这里的重点是先设置setFontColorIndex在设置setFontColor,原因是直接使用setFontColor会报数组越界异常,他源代码里面没有初始化数组,而setColorIndex会初始数组

cf_W_1.setFontColorIndex(xssfColor.getIndex());
		cf_W_1.setFontColor(xssfColor);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值