Rectangle 27 0

How to change color of a cell of an existing excel (xlsx or xls) in java?


HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
cell.setCellStyle(colorStyle);
colorStyle = workbook.createCellStyle();
colorStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
colorStyle.setFillForegroundColor(HSSFColor.RED.index);
workbook.write(outputStream);

Finally, you write the workbook to a new file, through a FileOutputStream

Now you have to loop through the rows in all workbooks, identify the duplicates, and change the style of those cells:

This answer is for .xls; the API for xlsx is very similar, though. Reading the Excel file is easy, open a FileInputstream for the file, then create a HSSFWorkbook

To change to color of some cells, you first have to create a Style with this color (changing the style of a cell will also change some other properties, like the font etc., so things might get a bit messy here, but that's how excel works)

Note