You are using the same HSSFCellStyle object for each cell--you need to
create a different one for each cell:

        for (int i=0; i<colors.length; i++) {
            row = sheet.createRow( (short) i);
            cell = row.createCell( (short) 0);
            HSSFCellStyle style1 = wb.createCellStyle();
// create a new style for this row
            style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            style1.setFillForegroundColor(colors[i]);
            cell.setCellStyle(style1);
            cell.setCellValue(colors[i]);
        }



Best regards,

Robert Lowe
http://RMLowe.com/




-----Original Message-----
From: Sergei P. Volin [mailto:[EMAIL PROTECTED]
Sent: Monday, October 13, 2003 1:37 AM
To: 'POI Users List'
Subject: Setting foreground colors to cells individually failed


Hi,

I'm trying to write the test code to set foreground colors to cells in a
column individually. But failed - the whole column is displaied in one
color, actually the last that was applyed to the cell style. What's wrong
with my code?
How to write this code correctly?

Thanks,
Sergei.

        short[] colors = {...}; // array of colors indexes like
'HSSFColor.AQUA.index' etc.

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        HSSFCellStyle style = wb.createCellStyle();
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i=0; i<colors.length; i++) {
            row = sheet.createRow( (short) i);
            cell = row.createCell( (short) 0);
            style.setFillForegroundColor(colors[i]);
            cell.setCellStyle(style);
            cell.setCellValue(colors[i]);
        }

        FileOutputStream out = new FileOutputStream("excel_colors.xls");
        wb.write(out);
        out.close();



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to