繁体   English   中英

如何使用C#渲染Excel单元格字体颜色?

[英]How to render the Excel cells font color with C#?

我需要在Excel中呈现一种颜色,当单元格内容等于“ true”时,字体颜色为绿色!当字体内容等于“ false”时,颜色为红色。这是我的代码:

private void colorRender(Worksheet workSheet)
    {
      for(int i=0;i<workSheet.Rows.Count;i++)
      {
        for(int j=0;j<workSheet.Columns.Count;j++)
        {
          if(workSheet.Columns.Name=="校验结果")
          {
            if(workSheet.Cells[i,j].ToString()=="false")
            {
              //if the cells was equal to false,set the font color red,others green. 
              //Microsoft.Office.Interop.Excel.Range range=Microsoft.Office.Interop.Excel.Worksheets.
            }
          }
        }
      }
    }

当我在此处书写时遇到问题:工作表不包含get_range函数。

您必须强制转换要作为范围的Cell对象,然后尝试设置字体样式。

if (workSheet.Columns.Name == "校验结果")
{
    Excel.Range range = workSheet.Cells[i, j] as Excel.Range;
    if (range.Value2.ToString() == "false")
    {
        range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    }
    else
    {
        range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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