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

后端 未结 3 1888
南笙
南笙 2021-02-05 19:34

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

相关标签:
3条回答
  • 2021-02-05 19:49

    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;
    
    0 讨论(0)
  • 2021-02-05 19:55

    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)

    0 讨论(0)
  • 2021-02-05 20:09

    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;
    
    0 讨论(0)
提交回复
热议问题