java - Setting color of each table cell in a row -
i have servlet taking in set of data, processing it, , writing either excel file or text page depending on form submission. processing occurring in respective model using apache poi when processing excel. i'm trying modify color code rows based on data contained, however, after applying color row, when write workbook fileoutputstream, colors not there. i'm processing data excel file 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 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(); (integer key : keyset) { xssfrow row = sheet.createrow(rowindex++); string line = formattedreport.get(key); string[] strarr = line.split(","); int cellcount = 0; (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 excel file still, formatting isn't there. doing wrong?
the solution me set style in cell creation , value setting loop. otherwise, each cell when created overwrites row formatting why failing me. case if set row formatting after cell creation.
Comments
Post a Comment