I have a servlet that is taking in a set of data, processing it, and writing it either into an excel file or as a text page depending on the form submission. All the processing is occurring in a respective model using the Apache POI when processing Excel. I'm trying to modify this so that it will color code the rows based on the data contained, however, after applying the color to the row, when I write the workbook to the fileoutputstream, the colors are not there. I'm processing the data into the excel file as such:
MCVE
package mcve;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Set;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class MCVE {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
LinkedHashMap<Integer, String> testReport = new LinkedHashMap<>();
testReport.put(0, "CPU");
testReport.put(1, "App");
testReport.put(2, "Other");
testReport.put(3, "Memory");
testReport.put(4, "Power");
testReport.put(5, "Disk");
File file = new File("C:/SplunkTesting/report.xlsx");
FileOutputStream out = new FileOutputStream(file);
writeToExcel(testReport).write(out);
out.flush();
out.close();
}
private static XSSFWorkbook writeToExcel(LinkedHashMap<Integer, String> formattedReport) {
try {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
int rowIndex = sheet.getLastRowNum();
Set<Integer> keySet = formattedReport.keySet();
for (Integer key : keySet) {
XSSFRow row = sheet.createRow(rowIndex++);
String line = formattedReport.get(key);
String[] strArr = line.split(",");
int cellCount = 0;
for (String str : strArr) {
XSSFCell cell = row.createCell(cellCount++);
cell.setCellValue(str);
}
XSSFCellStyle style = workbook.createCellStyle();
if (line.contains("App")) {
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
} else if (line.contains("CPU")) {
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
} else if (line.contains("Disk")) {
style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
} else if (line.contains("Memory")) {
style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
} else if (line.contains("Network")) {
style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
} else if (line.contains("Power")) {
style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
} else {
style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
}
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
row.setRowStyle(style);
}
return workbook;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I get the excel file still, but the formatting isn't there. What am I doing wrong?