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]