C# Excel with NPOI:获取单元格的样式(背景色)
我正在使用NPOI解析一个带有C#(VS 2013)的Excel文件。我想得到一个细胞的背景颜色,我正在这样做: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
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;通过检查以下位置的测试获得: