Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel with NPOI:获取单元格的样式(背景色)_C#_Excel_Npoi - Fatal编程技术网

C# Excel with NPOI:获取单元格的样式(背景色)

C# Excel with NPOI:获取单元格的样式(背景色),c#,excel,npoi,C#,Excel,Npoi,我正在使用NPOI解析一个带有C#(VS 2013)的Excel文件。我想得到一个细胞的背景颜色,我正在这样做: IEnumerator rows = sheet.GetRowEnumerator(); while (rows.MoveNext()) { IRow row = (IRow)rows.Current; foreach (ICell cell in row.Cells) { if (cell.CellStyle.FillBackground

我正在使用NPOI解析一个带有C#(VS 2013)的Excel文件。我想得到一个细胞的背景颜色,我正在这样做:

IEnumerator rows = sheet.GetRowEnumerator();

while (rows.MoveNext())
{
    IRow row = (IRow)rows.Current;

    foreach (ICell cell in row.Cells)
    {
        if (cell.CellStyle.FillBackgroundColor == 64)
        {
            ...
        }
    }
}
问题是,不管单元格的背景颜色是什么(黄色、绿色、完全没有颜色),FillBackgroundColor的值总是64。看来,那不是储存颜色的地方。那我怎么得到它呢

Edith说:所有单元格的cell.CellStyle.Index属性都不同

提前感谢,,
Frank

原因是,在Excel中,CellStyle.FillBackgroundColor不是单元格的FillColor。要检查它,首先确保它有一个填充(CellStyle.FillPattern),然后检查CellStyle.FillForegroundColorColor属性。f、 e

if (cell.CellStyle.FillPattern == FillPattern.SolidForeground)
   byte[] cellBackground = cell.CellStyle.FillForegroundColorColor.RGB;

您可能必须根据电子表格文件的类型强制转换它。((XSSFCellStyle)row.GetCell(j.CellStyle).FillForegroundXSSFColor.RGB;通过检查以下位置的测试获得: