XLS - Conditional Formatting - Java POI Example

salvador picture salvador · May 9, 2013 · Viewed 8k times · Source

:) Finally after the research I found the solution to my problem, which is not yet satisfied

I would like to use conditional formatting to show a line with a yellow color if column B and C to the same line does not have the same value. this is the marco on VBA I'm not using it just for help me undrstund

    For i = 3 To fin Step 1
        Range("C" & i).Select
        Selection.FormatConditions.Delete
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
            Formula1:="=B" & i
        Selection.FormatConditions(1).Interior.ColorIndex = 6

this is My Methode java .it is like this But

FileInputStream file = new FileInputStream(new File("D://DEQ//"+selectitem.getRefDeq()+"//Comparatif.xls"));        
        HSSFWorkbook workbook1 = new HSSFWorkbook(file);
        HSSFSheet sheet1 = workbook1.getSheet("page1");
                HSSFSheetConditionalFormatting cf =sheet1.getSheetConditionalFormatting();
        HSSFConditionalFormattingRule cfrole=cf.createConditionalFormattingRule(org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator.NOT_EQUAL, "120");

        HSSFPatternFormatting fill_pattern = cfrole.createPatternFormatting();
        fill_pattern.setFillBackgroundColor(IndexedColors.YELLOW.index);

        CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("B17:B26")};
        cf.addConditionalFormatting(my_data_range,cfrole);
FileOutputStream out = new FileOutputStream(new File("D://DEQ//"+selectitem.getRefDeq()+"//Comparatif.xls"));
        workbook1.write(out);
        out.close();

this example work well it shows me the yellow colored line. but as you've already seen I have values ​​that are a = 120 which is displayed with the color yellow. problem is that I think the values ​​are not in the digital format in my page .. this is not a problem

My real problem is the value that I have to comprare with, I do not know how I have expressed that each box B and C of the same line.   I put here a single value = 120 only for test

How should I do .. at the value comprarer thank you in advance :)

Answer

salvador picture salvador · May 10, 2013

Finaly this is my solution it works fine ..think you @Philip

FileInputStream file = new FileInputStream(new File("D://DEQ//"+selectitem.getRefDeq()+"//Comparatif.xls"));        
            HSSFWorkbook workbook1 = new HSSFWorkbook(file);
            HSSFSheet sheet1 = workbook1.getSheet("Comparatif");
            //Get first sheet from the workbook

            HSSFSheetConditionalFormatting cf =sheet1.getSheetConditionalFormatting();
            int i;
            i=17;
            for(;i<=ligne;i++){
            HSSFConditionalFormattingRule cfrole=cf.createConditionalFormattingRule(ComparisonOperator.NOT_EQUAL,"$C$"+i ); 
            HSSFPatternFormatting fill_pattern = cfrole.createPatternFormatting();
            fill_pattern.setFillBackgroundColor(IndexedColors.YELLOW.index);
            CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("B"+i+":B"+i)};
            cf.addConditionalFormatting(my_data_range,cfrole);
            }
            for(i=17;i<=ligne;i++){
                HSSFConditionalFormattingRule cfrole=cf.createConditionalFormattingRule(ComparisonOperator.NOT_EQUAL,"$B$"+i ); 
                HSSFPatternFormatting fill_pattern = cfrole.createPatternFormatting();
                fill_pattern.setFillBackgroundColor(IndexedColors.YELLOW.index);
                CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("C"+i+":C"+i)};
                cf.addConditionalFormatting(my_data_range,cfrole);
                }
    FileOutputStream out = new FileOutputStream(new File("D://DEQ//"+selectitem.getRefDeq()+"//Comparatif.xls"));
            workbook1.write(out);
            out.close();