1.合并单元格
// 合并单元格(开始行, 结束行, 开始列, 结束列)
CellRangeAddress regionRow0 = new CellRangeAddress(0, 0, 0, 10);
sheet.addMergedRegion(regionRow0);
2.单元格根据汉字自动匹配颜色
HSSFConditionalFormattingRule orange = scf.createConditionalFormattingRule(
ComparisonOperator.EQUAL, "\"橙色\"", null);
HSSFFontFormatting cf_orange = orange.createFontFormatting();
cf_orange.setFontColor(HSSFColor.HSSFColorPredefined.ORANGE.getColor());
HSSFConditionalFormattingRule brown = scf.createConditionalFormattingRule(
ComparisonOperator.EQUAL, "\"棕色\"", null);
HSSFFontFormatting cf_brown = brown.createFontFormatting();
cf_brown.setFontColor(HSSFColor.HSSFColorPredefined.BROWN.getColor());
HSSFConditionalFormattingRule black = scf.createConditionalFormattingRule(
ComparisonOperator.EQUAL, "\"黑色\"", null);
HSSFFontFormatting cf_black = black.createFontFormatting();
cf_black.setFontColor(HSSFColor.HSSFColorPredefined.BLACK.getColor());
HSSFConditionalFormattingRule[] cfRules = {cf_Red_rule, cf_Blue_rule, cf_violet_rule, dark_blue, light_blue,
dark_green, light_green, yellow, orange, brown, black};
//条件格式应用的单元格范围 (第四列,第一行到10000行)
CellRangeAddress[] regions = {new CellRangeAddress(1, 10000, 4, 4)};
for (HSSFConditionalFormattingRule rule : cfRules) {
scf.addConditionalFormatting(regions, rule);
}
3.设置列为下拉框
// 班次背景、字体颜色,赋值下拉框
String colour = "color";
Map<String, String> colourMap = new HashMap<>(4);
// map的value值为颜色名称字符串集合
colourMap.put(colour, ColourEnum.getColourName());
// 1000表示该列填充1000行
HSSFDataValidation backgroundValidation = ExcelUtil.createBox(colour, colourMap, 1, 1000, 3, 3);
HSSFDataValidation fontValidation = ExcelUtil.createBox(colour, colourMap, 1, 1000, 4, 4);
if (backgroundValidation != null) {
sheet.addValidationData(backgroundValidation);
sheet.addValidationData(fontValidation);
}
/**
* excel导出,有码值的数据使用下拉框展示。
* @param col 列名
* @param boxMap 码值集合
* @param firstRow 插入下拉框开始行号
* @param lastRow 插入下拉框结束行号
* @param firstCol 插入下拉框开始列号
* @param lastCol 插入下拉框结束行号
* @return
*/
public static HSSFDataValidation createBox(String col, Map<String, String> boxMap, int firstRow, int lastRow, int firstCol, int lastCol) {
HSSFDataValidation dataValidation = null;
//查询码值表
String cols = "";
if(null != boxMap.get(col)) {
cols = boxMap.get(col);
}
//设置下拉框
if(cols.length() > 0 && null != cols) {
String str[] = cols.split(",");
//指定行,列为下拉框
CellRangeAddressList cas = new CellRangeAddressList(firstRow , lastRow , firstCol , lastCol);
//创建下拉数据列
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(str);
//将下拉数据放入下拉框
dataValidation = new HSSFDataValidation(cas, dvConstraint);
}
return dataValidation;
}
条件1、2、3 得出样式为: