【问题标题】:NPOI does not change cell´s font colorNPOI 不会改变单元格的字体颜色
【发布时间】:2017-11-13 15:13:30
【问题描述】:

我正在尝试有条件地更改单元格的字体颜色。这是我最后一次尝试:

IWorkbook wb = null;

using (FileStream _fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    wb = WorkbookFactory.Create(_fileStream);
    _fileStream.Close();
}



ISheet sheet = wb.GetSheet(sheetName);
IFont font = wb.CreateFont();
...
...

// within a loop
ICell cell = sheet.GetRow(r).GetCell(col);
if (integrity < 1)
{
    ICellStyle redStyle = cell.CellStyle;
    font.Color = IndexedColors.Red.Index;
    redStyle.SetFont(font);
    cell.CellStyle = redStyle;
}
else
{
    ICellStyle normalStyle = cell.CellStyle;
    font.Color = XSSFFont.DEFAULT_FONT_COLOR;
    normalStyle.SetFont(font);
    cell.CellStyle = normalStyle;
}                        

但是,满足条件时字体不会改变。似乎该样式适用于所有单元格,而不是我在循环中得到的单元格。我已经阅读了一些与此问题相关的问题,但我无法使其正常工作。

这项新尝试正在格式化所有单元格。不管是否满足条件

ICellStyle redStyle = cell.CellStyle;
font.Color = IndexedColors.Red.Index;             
redStyle.SetFont(font);    

//This is how I am trying to change cells format 
if (integrity < 1)
{
    cell.CellStyle.SetFont(font);
} 

Joao 响应将使用“normalStyle”格式化所有单元格

【问题讨论】:

    标签: c# npoi


    【解决方案1】:

    默认情况下,每个单元格都将使用相同的 CellStyle 对象。如果您想为不同的单元格设置不同的样式,则必须创建不同的对象。

    ICellStyle redStyle = wb.CreateCellStyle();
    font.Color = IndexedColors.Red.Index;
    redStyle.SetFont(font);
    
    ICellStyle normalStyle = wb.CreateCellStyle();
    font.Color = XSSFFont.DEFAULT_FONT_COLOR;
    normalStyle.SetFont(font);
    
    // within a loop
    ICell cell = sheet.GetRow(r).GetCell(col);
    if (integrity < 1)
    {
        cell.CellStyle = redStyle;
    }
    else
    {
        cell.CellStyle = normalStyle;
    }                        
    

    (注意:我根本没有测试过这段代码。我忘记了 CreateCellStyle 是否像那样工作。但它至少应该为您指明正确的方向。)

    【讨论】:

    • 感谢您的回复!但是它没有按预期工作。根据您的回复,我上次尝试编辑了我的问题
    • 您的编辑不可能工作。请注意,我的代码在循环外部创建ICellStyle 对象,并直接从工作簿创建它们。您不能只访问cell.CellStyle 属性并修改其属性,否则您将一直在处理同一个对象。
    【解决方案2】:

    对于 XSSF 格式,使用此方法创建字体,然后将其分配给单元格

    XSSFFont defaultFont = (XSSFFont)workbook.CreateFont();
    defaultFont.FontHeightInPoints = (short)10;
    defaultFont.FontName = "Arial";
    defaultFont.Color = IndexedColors.Black.Index;
    defaultFont.IsBold = false;
    defaultFont.IsItalic = false;
    defaultFont.Boldweight = 700;
    
    XSSFCellStyle defaultStyle = (XSSFCellStyle)workbook.CreateCellStyle();
    defaultStyle.SetFont(defaultFont);
    

    【讨论】:

      【解决方案3】:

      所以我最终预先定义了一个样式,我将其设置为满足条件的单元格。

      IFont redfont = wb.CreateFont();
      redfont.Color = IndexedColors.Red.Index;
      redfont.FontHeight = 8;
      ICellStyle style = wb.CreateCellStyle();
      
      ...
      if (integrity < 1)
      {
          style.CloneStyleFrom(cell.CellStyle);
          style.SetFont(redfont);
          cell.CellStyle = style;
      }
      

      感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 2011-09-23
        • 1970-01-01
        • 2021-09-30
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 1970-01-01
        • 2014-06-16
        相关资源
        最近更新 更多