2014-05-05 112 views
0

這是我的一段代碼,但問題在於它沒有突出顯示特定的重複值,它突出顯示所有值,如果它在A5單元格中找到重複值並且A6,但如果我在A8和A10等其他單元格上進行更改,它不會以紅色突出顯示。如何在Excel工作表中突出顯示重複值

這是代碼:

HSSFSheetConditionalFormatting mycf = new_hire.getSheetConditionalFormatting(); 

HSSFConditionalFormattingRule rule = mycf.createConditionalFormattingRule("COUNTIF($A$5:$A$500,A5)>1"); 
HSSFFontFormatting mypattern = rule.createFontFormatting(); 
mypattern.setFontStyle(false, true);  
mypattern.setFontColorIndex(IndexedColors.RED.getIndex()); 
CellRangeAddress[] range = { CellRangeAddress.valueOf("A5:A500") }; 
mycf.addConditionalFormatting(range,rule); 

回答

0

COUNTIF($A$5:$A$500,A5)>1只會在單元格A5匹配別的地方適用。

所以我會通過爲每個單元格單獨的條件格式規則來解決這個問題。我生成這個是這樣的:

HSSFConditionalFormatting mycf = new_hire.getSheetConditionalFormatting(); 
for(int x=4; x<500; x++) { 
    HSSFRow row = new_hire.getRow(x); 
    if(null == row) { 
     row = new_hire.createRow(x); 
    } 
    HSSFCell cell = row.getCell(0); // 0 for column A 
    if(null == cell) { 
     cell = row.createCell(0); 
    } 
    String cellId = "A"+(x+1); 
    CellRangeAddress[] range = new CellRangeAddress(x, x, 0, 0); // 0 for column A 
    HSSFConditionalFormattingRule rule = highlightIfMatch(mycf, cellId); 
    range.addConditionalFormatting(range, rule); 
} 

private HSSFSheetConditionalFormattingRule highlightIfMatch(HSSFSheetConditionalFormatting mycf, String cellId) { 
    HSSFConditionalFormattingRule rule = mycf.createConditionalFormattingRule("COUNTIF($A$5:$a$500,"+cellId+")>1"); 
    //Go on to set your FontFormatting here 
    return rule; 
} 

然後替換爲您的代碼

相關問題