Unable to set FillBackgroundColor using HSSFWorkbook in C#

Rahul Kiwitech
Rahul K...
Participant
292 Points
26 Posts

Hi, I am using HSSFWorkbook to generate report in Excel. I am trying to set background color of title but it is showing only black background.

My code is as

IWorkbook wb = new HSSFWorkbook();
ICellStyle style = wb.CreateCellStyle();
style.BorderBottom = BorderStyle.Thin;
style.BorderTop = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
style.FillBackgroundColor = HSSFColor.LightGreen.Index;
style.FillPattern = FillPattern.SolidForeground;
 
ICellStyle hstyle = wb.CreateCellStyle();
hstyle.BorderBottom = BorderStyle.Thin;
hstyle.BorderTop = BorderStyle.Thin;
hstyle.BorderLeft = BorderStyle.Thin;
hstyle.BorderRight = BorderStyle.Thin;
hstyle.Alignment = HorizontalAlignment.Center;
 
IFont bfont = wb.CreateFont();
 
bfont.Boldweight = (short)FontBoldWeight.Bold;
hstyle.SetFont(bfont);
hstyle.FillBackgroundColor = HSSFColor.LightGreen.Index;
hstyle.FillPattern = FillPattern.SolidForeground;
 
ICellStyle datestyle = wb.CreateCellStyle();
datestyle.BorderBottom = BorderStyle.Thin;
datestyle.BorderTop = BorderStyle.Thin;
datestyle.BorderLeft = BorderStyle.Thin;
datestyle.BorderRight = BorderStyle.Thin;
datestyle.DataFormat = wb.CreateDataFormat().GetFormat("MMMM dd, yyyy");
datestyle.FillBackgroundColor = HSSFColor.LightGreen.Index;
datestyle.FillPattern = FillPattern.SolidForeground;
 
ICellStyle Titlestyle = wb.CreateCellStyle();
Titlestyle.BorderBottom = BorderStyle.Thin;
Titlestyle.BorderTop = BorderStyle.Thin;
Titlestyle.BorderLeft = BorderStyle.Thin;
Titlestyle.BorderRight = BorderStyle.Thin;
Titlestyle.Alignment = HorizontalAlignment.Center;
Titlestyle.SetFont(bfont);
Titlestyle.FillBackgroundColor = HSSFColor.Green.Index;
Titlestyle.FillPattern = FillPattern.SolidForeground;
ISheet sheet = wb.CreateSheet("Orders");
IRow row = sheet.CreateRow(0);
AddStyledCell(row, 0, Titlestyle).SetCellValue(ReportTitle);
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));

Thanks

Views: 13928
Total Answered: 3
Total Marked As Answer: 0
Posted On: 23-Aug-2016 00:13

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Editor
1382 Points
14 Posts
         

You need to set foreground color as well with same color.

Titlestyle.FillBackgroundColor = HSSFColor.Green.Index;
Titlestyle.FillPattern = FillPattern.SolidForeground;
Titlestyle.FillForegroundColor = HSSFColor.Green.Index;
Posted On: 23-Aug-2016 00:19
Rahul Kiwitech
Rahul K...
Participant
292 Points
26 Posts
         

Thanks, great, now it is working. But it has limited color. I want to bg color with rgb and tried following code but showing black background only.

ICellStyle Titlestyle = wb.CreateCellStyle();
Titlestyle.BorderBottom = BorderStyle.Thin;
Titlestyle.BorderTop = BorderStyle.Thin;
Titlestyle.BorderLeft = BorderStyle.Thin;
Titlestyle.BorderRight = BorderStyle.Thin;
Titlestyle.Alignment = HorizontalAlignment.Center;
Titlestyle.SetFont(bfont);
byte[] rgb = new byte[3];
rgb[0] = (byte)76;
rgb[1] = (byte)84;
rgb[2] = (byte)61;
XSSFColor myColor = new XSSFColor(rgb);
Titlestyle.FillBackgroundColor = myColor.Indexed;
Titlestyle.FillPattern = FillPattern.SolidForeground;
Titlestyle.FillForegroundColor = myColor.Indexed;

 

Posted On: 24-Aug-2016 21:47
Smith
Smith
None
2568 Points
74 Posts
         

Hey guys,

I am also facing the same problem. Is Anyone other found solution?

Posted On: 28-Aug-2016 02:42
 Log In to Chat