Excel 使用 NPOI:获取单元格的样式(背景色)
Excel with NPOI: Get the style (backgroundcolor) of a Cell
我正在使用 NPOI 通过 C# (VS 2013) 解析 Excel-File。我想获取单元格的背景颜色,我正在这样做:
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 属性 都不同
提前致谢,
弗兰克
原因是,在Excel中,CellStyle.FillBackgroundColor不是Cell的FillColor。要检查它,首先确保它有填充 (CellStyle.FillPattern),然后检查 CellStyle.FillForegroundColorColor 属性。 f.e.
if (cell.CellStyle.FillPattern == FillPattern.SolidForeground)
byte[] cellBackground = cell.CellStyle.FillForegroundColorColor.RGB;
我正在使用 NPOI 通过 C# (VS 2013) 解析 Excel-File。我想获取单元格的背景颜色,我正在这样做:
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 属性 都不同
提前致谢, 弗兰克
原因是,在Excel中,CellStyle.FillBackgroundColor不是Cell的FillColor。要检查它,首先确保它有填充 (CellStyle.FillPattern),然后检查 CellStyle.FillForegroundColorColor 属性。 f.e.
if (cell.CellStyle.FillPattern == FillPattern.SolidForeground)
byte[] cellBackground = cell.CellStyle.FillForegroundColorColor.RGB;