简体   繁体   中英

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

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?

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM