使用java更改excel单元格颜色

时间:2013-10-31 23:53:12

标签: java excel colors apache-poi

我必须使用简单的公式验证细胞,如果无效,请将细胞颜色更改为黄色或红色,这无关紧要。

到目前为止,我已经看过并尝试过这个:

SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

 // formula = =SUMA(L13+M13+N13+O13)
    //=SUMA(M4+N4+O4+P4+L:L
    ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.EQUAL, "4");
    PatternFormatting fill2 = rule2.createPatternFormatting();
    fill2.setFillBackgroundColor(IndexedColors.GREEN.index);
    fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

    CellRangeAddress[] regions = { CellRangeAddress.valueOf("L2:L5")};

    sheetCF.addConditionalFormatting(regions, rule2);
}

如果值为4,那我会改变颜色,我知道,但是

如何应用以下总和的公式:

enter image description here

我需要验证列L的所有行,m2 + n2 + o2 + p2和m3 + n3 + o3 + p3等的总和......

仅供参考,我从数据库中获取值,并且值在十进制的图片中,但我在Java中将它们作为int得到它们:

while((j <  fallas) && rset.next() ){
  row = sheet.createRow(j+1);
  row.createCell(0).setCellValue((rset.getBigDecimal(1)).toString());
  row.createCell(1).setCellValue(rset.getString(2));
  row.createCell(2).setCellValue(rset.getString(3));
  row.createCell(3).setCellValue(rset.getString(4));
  row.createCell(4).setCellValue(rset.getString(5));
  row.createCell(5).setCellValue(rset.getString(6));
  row.createCell(6).setCellValue(rset.getString(7));
  row.createCell(7).setCellValue((rset.getBigDecimal(8)).toString());
  row.createCell(8).setCellValue((rset.getBigDecimal(9)).toString());
  row.createCell(9).setCellValue((rset.getBigDecimal(10)).toString());
  row.createCell(10).setCellValue(rset.getString(11));

  //row.createCell(11).setCellFormula("SUMA(L13+M13+N13+O13)");

  row.createCell(11).setCellValue(String.valueOf((rset.getBigDecimal(12)).toString()));
  row.createCell(12).setCellValue(String.valueOf((rset.getBigDecimal(13)).toString()));
  row.createCell(13).setCellValue(String.valueOf((rset.getBigDecimal(14)).toString()));
  row.createCell(14).setCellValue(String.valueOf((rset.getBigDecimal(15)).toString()));
  row.createCell(15).setCellValue(String.valueOf((rset.getBigDecimal(16)).toString()));
  row.createCell(16).setCellValue((rset.getString(17)));
  row.createCell(17).setCellValue((rset.getString(18)));
  row.createCell(18).setCellValue(rset.getString(19));
  row.createCell(19).setCellValue(rset.getString(20));
  row.createCell(20).setCellValue("-");
  row.createCell(21).setCellValue(new java.sql.Date(rset.getDate(22).getTime()).toString());
  row.createCell(22).setCellValue((rset.getBigDecimal(23)).toString());
  row.createCell(23).setCellValue("-");
  row.createCell(24).setCellValue("-");
  row.createCell(25).setCellValue("-");
  row.createCell(26).setCellValue("-");
  row.createCell(27).setCellValue((rset.getBigDecimal(28)).toString());
  row.createCell(28).setCellValue((rset.getBigDecimal(29)).toString());
  row.createCell(29).setCellValue(rset.getString(30));
  row.createCell(30).setCellValue(rset.getString(31));
  row.createCell(31).setCellValue(rset.getString(32));

L,M,N,O,P是createCell(11),12,13,14等。

如果我把公式放在“createConditionalFormattingRule”上它没有做任何事情:(请帮帮我!

1 个答案:

答案 0 :(得分:2)

你在验证这笔款项是什么?

假设这些单元格的总和与某些数字进行比较,您可以尝试这样做:

// Condition 1: Formula Is   =SUM($M2+$N2+$O2+$P2) > 75   
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("SUM($M2+$N2+$O2+$P2)>75");

一起

SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();

// Condition 1: Formula Is   =SUM($M2+$N2+$O2+$P2) > 75   
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("SUM($M2+$N2+$O2+$P2)>75");

PatternFormatting fill1 = rule1.createPatternFormatting();

fill1.setFillBackgroundColor(IndexedColors.RED.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

CellRangeAddress[] regions = {
    CellRangeAddress.valueOf("L2:L5")
};

sheetCF.addConditionalFormatting(regions, rule1);

这是一个link,它显示了使用java创建条件格式的多种方法。