2016-03-23 19 views
0

için hücre arkaplanı ekliyor. Daha önce .xls (Apache POI tarafından) oluşturdum, tekrar açtım ve arka plan rengini hücre değerine göre değiştiriyorum. Sorun şu ki, bu arka plan tüm hücrelerde değil, sadece ilk kabaca 50 sıra, diğer kalanlar ise beyaz bg ile değiştirildi. Ben, değerlerini, sıra numaralarını dökümü çalıştı döngüsü için birinde Fonksiyon mark()POI, yalnızca ilk 50 satır

import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Font; 

public class Xls { 

    private FileOutputStream fileOut; 
    private Sheet xlsSheet; 
    private HSSFWorkbook xlsWorkbook; 
    private CellStyle cellStyle; 

    public Xls(String path) { 
    try { 
     fileOut = new FileOutputStream(path); 
     this.xlsWorkbook = new HSSFWorkbook(); 
     this.xlsSheet = xlsWorkbook.createSheet("test"); 

     cellStyle = this.xlsWorkbook.createCellStyle(); 

     Row row1 = xlsSheet.createRow((int) 0); 

     this.xlsSheet.autoSizeColumn(0, true); 
     this.xlsSheet.autoSizeColumn(1, true); 
     this.xlsSheet.autoSizeColumn(2, true); 
     this.xlsSheet.autoSizeColumn(3, true); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    public void mark(int rowNumber, String status) { 
     short color = Constants.getColor(status); 

     CellStyle style = this.xlsWorkbook.createCellStyle(); 

     style.setFillForegroundColor(color); 
     style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
     //TODO!!! Why filling color only in first 50 rows?! 
//   System.out.println("Changing row " + sameRows.get(rowNumber) + " status is:'" + status + "' color:" + color); 
      Row row = xlsSheet.getRow(sameRows.get(rowNumber)); 
      Cell statusCell = null; 
      if (!isCellEmpty(row.getCell(3))) { 
       statusCell = row.getCell(3); 
      } else { 
       statusCell = row.createCell(3); 
      } 

      statusCell.setCellValue(status); 
      statusCell.setCellStyle(style); 
     } 
    } 

} 

nerede hata olabilir bir fikrin var mı ... renk ve hiçbir şey inşaat assing tekrar tekrar uç yinelerler xls denedi? NOT: setCellValue() - tüm alanların uygun değerleri vardır.

cevap

2

CellStyles her Hücre için yeniden oluşturulmamalı, bir Excel dosyasında sınırlı bir kaynaktır (sınır Excel'in kendisi tarafından uygulanır), bu nedenle stil nesnesini yalnızca bir kez oluşturun ve tüm hücreler için yeniden kullanın. aynı stil olmalı.

+0

Teşekkürler, daha fazla renk var, bu yüzden az sayıda değişken oluşturdum ve çalışıyor ;-) –