You know you've asked the same question in three threads.

You need to have:


       HSSFCellStyle stringStyleOdd = wb.createCellStyle();
       HSSFCellStyle intStyleOdd = wb.createCellStyle();
       HSSFCellStyle longStyleOdd = wb.createCellStyle();
       HSSFCellStyle doubleStyleOdd = wb.createCellStyle();
       HSSFCellStyle dateStyleOdd = wb.createCellStyle();
       HSSFCellStyle timeStampStyleOdd = wb.createCellStyle();

AND

       HSSFCellStyle stringStyleEven = wb.createCellStyle();
       HSSFCellStyle intStyleEven = wb.createCellStyle();
       HSSFCellStyle longStyleEven = wb.createCellStyle();
       HSSFCellStyle doubleStyleEven = wb.createCellStyle();
       HSSFCellStyle dateStyleEven = wb.createCellStyle();
       HSSFCellStyle timeStampStyleEven = wb.createCellStyle();

One of each data type for each.

Also if different doubles have different numeric formats then you'll need a unique style for each format.

Maybe doubleStyle999o99Even, doubleStyle999o9Even, doubleStyle999o99Odd, doubleStyle999o9Odd

Completely set up all of your possible styles as a unique style before your loop.

Then you call according to the needs of the field.

Do NOT keep changing a style record for each cell as you iterate. When you do that you end up with the last version of the style record for every cell that you have assigned that style record no matter what state that style record was in when you called it.

You are thinking that you are setting the state of the cell with that style, but that's NOT how it works. When you assign a style you are linking objects - a style object and a cell object. You can then change either.

I hope this helps.

Regards,
Dave

On Jun 10, 2008, at 12:02 AM, suriz4u wrote:


Hi all
    I have problem with coloring cells.I should have a color for every
alternative row.So i tried in the following way,but iam getting same aqua
color for every row.Iam pasting my code .Can any one help me out
please.........

public void makeExcel(List list,String fields[][],OutputStream out) throws
Exception
   {
       HSSFWorkbook wb = new HSSFWorkbook();
       HSSFSheet sheet = wb.createSheet();
       HSSFRow row = null;
       HSSFCell cell = null;
       HSSFCellStyle columnHeaderStyle = wb.createCellStyle();
       HSSFDataFormat dataFormat = wb.createDataFormat();
       HSSFCellStyle stringStyle = wb.createCellStyle();
       HSSFCellStyle intStyle = wb.createCellStyle();
       HSSFCellStyle longStyle = wb.createCellStyle();
       HSSFCellStyle doubleStyle = wb.createCellStyle();
       HSSFCellStyle dateStyle = wb.createCellStyle();
       HSSFCellStyle timeStampStyle = wb.createCellStyle();
       HSSFFont normalFont = wb.createFont();
       HSSFFont boldFont = wb.createFont();
       normalFont.setFontHeightInPoints((short) 9);
       boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

       columnHeaderStyle.setFont(boldFont);
       stringStyle.setFont(normalFont);
       intStyle.setFont(normalFont);
       longStyle.setFont(normalFont);
       doubleStyle.setFont(normalFont);
       dateStyle.setFont(normalFont);
       timeStampStyle.setFont(normalFont);

wb.setSheetName(0, "Test1", HSSFWorkbook.ENCODING_COMPRESSED_UNICODE
);
       short fieldSize=0;
       int rownum=0;
       row = sheet.createRow(rownum++);
       for(short j=0;j<fields.length;j++)
           {
               cell=row.createCell(j);
               cell.setCellValue(fields[j][1]);
               cell.setCellStyle(columnHeaderStyle);
           }
       System.out.println("Start time"+  new java.util.Date());

       IBean iBean=null ;
       Iterator it =list.iterator();
       while(it.hasNext())
          {
            row = sheet.createRow(rownum++);
            iBean = (IBean)it.next();
            if(rownum>=maxRowCount )
            {
              rownum=0;
sheet= wb.createSheet("Test"+(wb.getNumberOfSheets() +1));
              row = sheet.createRow(rownum++);
              wb.getSheet("Test"+((wb.getNumberOfSheets())+1));
               for(short j=0;j<fields.length;j++)
               {
                   cell=row.createCell(j);
                   cell.setCellValue(fields[j][1]);
                   cell.setCellStyle(columnHeaderStyle);
               }
               row = sheet.createRow(rownum++);
            }
            for(short i=0;i<fields.length;i++)
            {
               try
               {
                   Object
value=PropertyUtils.getNestedProperty(iBean,fields[i][0]);
                   if(value == null)
value=""; // initialize the null values to empty.
                   cell=row.createCell(i);
                   if(value instanceof Double)
                   {
                       if(!(rownum%2==0))
                         {

doubleStyle.setFillForegroundColor(HSSFColor.AQUA.index);

doubleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                         }
                       else

doubleStyle.setFillForegroundColor(HSSFColor.WHITE.index);
if(fields[i][2]!=null && fields[i] [2].length()>0)
                           {

doubleStyle.setDataFormat(dataFormat.getFormat(fields[i][2]));
                              cell.setCellStyle(doubleStyle);
                            }
                       if(value.equals(""))
                           cell.setCellValue("");
                       else
                           cell.setCellValue(
((Double)value).doubleValue() );
                   }
                   if(value instanceof Integer)
                   {
                      if(!(rownum%2==0))
                      {

intStyle.setFillForegroundColor(HSSFColor.AQUA.index);

intStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                      }
                      else

intStyle.setFillForegroundColor(HSSFColor.WHITE.index);
if(fields[i][2]!=null && fields[i] [2].length()>0)
                            {

intStyle.setDataFormat(dataFormat.getFormat(fields[i][2]));
                              cell.setCellStyle(intStyle);
                            }
                           if(value.equals(""))
                               cell.setCellValue("");
                           else
                               cell.setCellValue(
((Integer)value).intValue() );
                   }
                   if(value instanceof java.sql.Date)
                   {
                       if(!(rownum%2==0))
                       {

dateStyle.setFillForegroundColor(HSSFColor.AQUA.index);

dateStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                       }

if(fields[i][2]!=null && fields[i] [2].length()>0)
                            {

dateStyle.setDataFormat(dataFormat.getFormat(fields[i][2]));
                              cell.setCellStyle(dateStyle);
                            }
                           if(value.equals(""))
                               cell.setCellValue("");
                           else
cell.setCellValue( (java.sql.Date)value );
                   }
                   if(value instanceof java.sql.Timestamp)
                       {
                           if(!(rownum%2==0))
                           {

timeStampStyle.setFillForegroundColor(HSSFColor.AQUA.index);

timeStampStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                           }
                           else

timeStampStyle.setFillForegroundColor(HSSFColor.WHITE.index);

                           if(fields[i][2]!=null &&
fields[i][2].length()>0)
                            {

timeStampStyle.setDataFormat(dataFormat.getFormat(fields[i][2]));
                              cell.setCellStyle(timeStampStyle);
                            }
                           if(value.equals(""))
                               cell.setCellValue("");
                           else
                               cell.setCellValue( new
java.util.Date(((java.sql.Timestamp)value).getTime()) ) ;
                       }
                   else if(value instanceof Long)
                        {
                           if(!(rownum%2==0))
                           {

longStyle.setFillForegroundColor(HSSFColor.AQUA.index);

longStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                           }

                             if(fields[i][2]!=null &&
fields[i][2].length()>0)
                            {

longStyle.setDataFormat(dataFormat.getFormat(fields[i][2]));
                              cell.setCellStyle(longStyle);
                            }
cell.setCellValue( ((Long)value).longValue()
);
                        }

                    else if(value instanceof String)
                        {
                           if(!(rownum%2==0))
                           {

stringStyle.setFillForegroundColor(HSSFColor.AQUA.index);

stringStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                           }

                           cell.setCellStyle(stringStyle);
                           cell.setCellValue(  (value).toString() );
                        }
                }
               catch(Exception ex)
               {
                   throw ex;
               }
            }
           }
           for(int i=0;i<wb.getNumberOfSheets();i++)
           {
               for(short j=0;j<fields.length;j++)
               {
                   wb.getSheetAt(i).autoSizeColumn((short)j);

               }
           }
        wb.write(out);
        System.out.println("End time"+  new java.util.Date());
   }


--
View this message in context: 
http://www.nabble.com/Problem-with-Colors-tp17747845p17747845.html
Sent from the POI - User mailing list archive at Nabble.com.


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




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

Reply via email to