Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Other Open Source Projects and the fly likes How to apply custom colors for HSSF Workbook Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to apply custom colors for HSSF Workbook" Watch "How to apply custom colors for HSSF Workbook" New topic
Author

How to apply custom colors for HSSF Workbook

Praveen Yendluri
Ranch Hand

Joined: Apr 13, 2011
Posts: 34
Hi Everyone,

I have to apply custom colors to Excel cells of HSSF Workbook. I have created cell style like but it is not applying expected clor

HSSFWorkbook hssfwb = (HSSFWorkbook)wb;
HSSFPalette palette = hssfwb.getCustomPalette();
short colorIndex = 45;
palette.setColorAtIndex(colorIndex, (byte)201, (byte)221, (byte)255);
CellStyle style = wb.createCellStyle();
style.setFillForegroundColor(colorIndex);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return style;
Please help me on this.

Thanks,
ypk
Olivier Rihoux
Greenhorn

Joined: Jun 03, 2013
Posts: 8
Hello,

I suspect that your problem is here : HSSFWorkbook hssfwb = (HSSFWorkbook)wb;

You define your style for hssfwb but you try to apply it on wb but these are two different objects

Try something like this :



Praveen Yendluri
Ranch Hand

Joined: Apr 13, 2011
Posts: 34
Hi Oliver,

Thanks, for your response.
But not worked out.

Is there any way to apply custom clors to cexcel cells with POI.

Thanks,
ypk
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41040
    
  43
You will need to supply more details for us to help you. Start by posting an SSCCE (<- that's a iink).


Ping & DNS - my free Android networking tools app
Praveen Yendluri
Ranch Hand

Joined: Apr 13, 2011
Posts: 34
Hello All,

I have requirement to apply colors to headers in Excel Report. I am poi 3.8v to create cells and applying styles.
In the below fashion I have created style and callingwhen ever required. But it is not working .

private static CellStyle setThinBorder_NoneColor(Workbook wb, DataFormat df) {

HSSFWorkbook hssfwb = (HSSFWorkbook)wb;
HSSFPalette palette = hssfwb.getCustomPalette();
short colorIndex = 45;
palette.setColorAtIndex(colorIndex, (byte)201, (byte)221, (byte)255);
HSSFCellStyle style = ((HSSFWorkbook)wb).createCellStyle();
style.setFillForegroundColor(colorIndex);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return style;
}

Can anyone help me how to apply custom colors with POI.

Thanks,
ypk
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41040
    
  43
That looks pretty much like the code you posted before, but it's not an SSCCE. The easier you make it for folks here to reproduce your problem, the likelier it is that someone will try to help. (If you post an SSCCE, I promise to run it and try to understand what's happening.)
Olivier Rihoux
Greenhorn

Joined: Jun 03, 2013
Posts: 8
please, try replacing



by

Praveen Yendluri
Ranch Hand

Joined: Apr 13, 2011
Posts: 34
Hi Olivier,

I have tried with the given sample piece, but not working.

Please help me.


Thanks,
ypk
Olivier Rihoux
Greenhorn

Joined: Jun 03, 2013
Posts: 8
to help more, i need not only the procedure returning thez style but also the main code where you use it
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41040
    
  43
Ulf Dittmer wrote:That looks pretty much like the code you posted before, but it's not an SSCCE. The easier you make it for folks here to reproduce your problem, the likelier it is that someone will try to help. (If you post an SSCCE, I promise to run it and try to understand what's happening.)
Praveen Yendluri
Ranch Hand

Joined: Apr 13, 2011
Posts: 34
Hi All,

My prolem was solved. Sample code below

HSSFCellStyle style = ((HSSFWorkbook)wb).createCellStyle();
HSSFPalette palette = ((HSSFWorkbook)wb).getCustomPalette();
palette.setColorAtIndex((short)57, (byte)255, (byte)228, (byte)225);
style.setFillForegroundColor(palette.getColor(57).getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

Thanks very much everyone for your kind help.

Regards,
ypk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to apply custom colors for HSSF Workbook
 
Similar Threads
Read a .xls,.xlsx file format using XSSF
pasing a workbook as parameter tp stored procedure
POI api to read MSExcel sheet
SVTableModel only for HSSF.
Writing on excel file using POI