我正在尝试使用NPOI 2.2.1或2.1.3.1读取Excel单元格的字体颜色 字体颜色并不重要NPOI总是说它是8(黑色)。
下面的代码片段 IWorkbook workbook = WorkbookFactory.Create(new FileStream(txtFileName.Text, FileMode.Open, FileAccess.Read));
ISheet worksheet = workbook.GetSheet("sheet1");
IRow row = worksheet.GetRow(0);
lblFontColor.Text = row.GetCell(0).CellStyle.GetFont(workbook).Color.ToString();
这是一个错误还是我做错了什么?
更新: 当读取.xlsx文件而不是较旧的.xls文件
时,这似乎只是一个问题答案 0 :(得分:0)
您可以在字体背景中设置任何颜色:
hstyle =(XSSFCellStyle) wb.CreateCellStyle();
color = new XSSFColor(new byte[] { 191,191,191});
hstyle.VerticalAlignment = VerticalAlignment.Center;
hstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
hfont = (XSSFFont)wb.CreateFont();
hfont.FontHeightInPoints = 12;
hfont.FontName = "Calibri";
hfont.Boldweight = (short)FontBoldWeight.Bold;
hfont.SetColor(color);
hstyle.SetFont(hfont);
crow = sheet.CreateRow(rowindex);
ccel = crow.CreateCell(0);
ccel.SetCellValue(Title);
ccel.CellStyle = hstyle;
答案 1 :(得分:0)
您可以通过获取IColor
和byte[3]
或XSSFFont
来获得字体颜色为XSSFColor
或RGB(HSSFFont
)。 HSSFColor
:
private static byte[] GetFontColourRGB(ICell cell)
{
IWorkbook workbook = cell.Row.Sheet.Workbook;
IFont font = cell.CellStyle.GetFont(workbook);
if (font is XSSFFont)
{
return (font as XSSFFont).GetXSSFColor().RGB;
} else if (font is HSSFFont && workbook is HSSFWorkbook)
{
return (font as HSSFFont).GetHSSFColor(workbook as HSSFWorkbook).RGB;
} else
{
return null;
}
}
鉴于RGB值,您可以将查找表用于颜色名称。