Can anybody help me with ExcelLibrary? I\'d like to set a cell background and font color, but I don\'t know how can I do it. I try to get access to a cell style, but I didn\'t f
I've looked into this library for you and found the following (warning - it's bad news!):
There is no released version of ExcelLibrary that allows access to cell colours.
In the unreleased source code there is a BackColor
property in the new CellStyle
class, however there is no property to represent foreground colour.
The BackColor
property is not persisted when the workbook is saved. It is only used to set the background colour of a cell when the workbook is loaded.
If use of colours is a requirement then use NPOI (as recommended by @jamietre). You can then set foreground and background colours like this:
HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();
// cell background
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
// font color
HSSFFont font1 = hssfworkbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
style1.SetFont(font1);
cell.CellStyle = style1;
I know you may be tied to ExcelLibrary, but have you looked into EPPlus? http://epplus.codeplex.com/
It will do exactly what you're asking - easily (and more)
I don't tested this but it seems that you the cell has a property called "Style" which defines the cellstyle. Here you can set the background color for a specific cell.
worksheet.Cells[0,0].Style.BackColor = Color.CornflowerBlue;