Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change cell style in an Excel file with ExcelLibrary?

Tags:

c#

excel

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 found it.

Anybody have any ideas?

like image 908
misho Avatar asked Jun 25 '10 10:06

misho


2 Answers

I've looked into this library for you and found the following (warning - it's bad news!):

  1. There is no released version of ExcelLibrary that allows access to cell colours.

  2. In the unreleased source code there is a BackColor property in the new CellStyle class, however there is no property to represent foreground colour.

  3. 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;
like image 58
Alex Angas Avatar answered Oct 26 '22 07:10

Alex Angas


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)

like image 39
iivel Avatar answered Oct 26 '22 06:10

iivel