2016-08-26 4 views
1

Ich verwende POI, um bestimmte Zeilen meiner Excel-Tabelle hervorzuheben, aber das scheint nicht zu funktionieren.Stil nicht in Excel mit POI angewendet

Ich googelte viel, aber das Problem scheint immer noch da zu sein.

Im Folgenden sind einige der Möglichkeiten, die ich überprüft habe und es hat nicht funktioniert.

HSSFFont font = (HSSFFont) sheet.getWorkbook().createFont(); 
font.setBold(true); 
style.setFont(font); 
style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
style.setFillForegroundColor(new HSSFColor.BLUE().getIndex()); 
cell.setCellStyle(style); 
//nextRow.setRowStyle(style); 

oder

HSSFCellStyle curStyle = (HSSFCellStyle) cell.getCellStyle(); 
curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
curStyle.setFillForegroundColor(HSSFColor.BLUE.index); 
cell.setCellStyle(curStyle); 

oder

HSSFPalette palette = ((HSSFWorkbook) sheet.getWorkbook()).getCustomPalette(); 
HSSFCellStyle style= (HSSFCellStyle) cell.getCellStyle(); 
HSSFColor myColor = palette.findSimilarColor(255, 0, 0); 
short palIndex = myColor.getIndex(); 
style.setFillForegroundColor(palIndex); 
cell.setCellStyle(style); 
+1

Sind Sie sicher, dass Sie die Datei verdrahten? Keine Ausnahmen? –

+0

Verwenden Sie nur ein Stilobjekt? Oder hast du viele? style1, style2, ... – DamienB

+0

Vielen Dank für das Aufzeigen. In der Tat schrieb ich nicht zu Excel zurück. – LookingForSolution

Antwort

0

Die erste Lösung funktioniert perfekt.

InputStream inp; 
HSSFWorkbook wb = null; 
HSSFSheet sheet = null; 
HSSFRow hssf_DataRow; 
HSSFCell cell = null; 

inp = new FileInputStream(FileName); 
wb = new HSSFWorkbook(inp); 
sheet = wb.getSheetAt(1); 

hssf_DataRow = sheet.createRow((short) 1); 

cell = hssf_DataRow.createCell(1); 

HSSFFont font = (HSSFFont) sheet.getWorkbook().createFont(); 
HSSFCellStyle style = (HSSFCellStyle) cell.getCellStyle(); 

font.setBold(true); 
style.setFont(font); 
style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
style.setFillForegroundColor(new HSSFColor.BLUE().getIndex()); 
cell.setCellStyle(style);