In a program I'm writing, I prepare an excel sheet ("master") and clone it multiple times. Each time I clone this master sheet I apply CellStyles to specific cells on the new cloned sheet. The problem is, every time I apply CellStyles to the cells on the sheet(s) I clone, the styles kept appearing on the master sheet and all other cloned sheets. Here's the snippet that shows how I'm doing the cloning:
for (Member member : allMembers) {
memberName = member.getFirstName();
// `schedule` below is a WorkBook object
XSSFSheet individualSheet = schedule.cloneSheet(0, memberName);
highlightMemberNames(individualSheet, memberName);
}
What highlightMemberNames(individualSheet, memberName) is doing is highlighting the cells in individualSheet that contain memberName. Here's it's code:
void highlightMemberNames(XSSFSheet individualSheet, String memberName) {
for (Row row : individualSheet) {
for (Cell cell : row) {
if (cell.getStringCellValue().equals(memberName)) {
cell.getCellStyle().setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index);
cell.getCellStyle().setFillForegroundColor(IndexedColors.LIGHT_GREEN.index);
cell.getCellStyle().setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.getCellStyle().setAlignment(HorizontalAlignment.CENTER);
}
}
}
}
Is there a way to avoid this problem?