【发布时间】:2017-11-13 15:13:30
【问题描述】:
我正在尝试有条件地更改单元格的字体颜色。这是我最后一次尝试:
IWorkbook wb = null;
using (FileStream _fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
wb = WorkbookFactory.Create(_fileStream);
_fileStream.Close();
}
ISheet sheet = wb.GetSheet(sheetName);
IFont font = wb.CreateFont();
...
...
// within a loop
ICell cell = sheet.GetRow(r).GetCell(col);
if (integrity < 1)
{
ICellStyle redStyle = cell.CellStyle;
font.Color = IndexedColors.Red.Index;
redStyle.SetFont(font);
cell.CellStyle = redStyle;
}
else
{
ICellStyle normalStyle = cell.CellStyle;
font.Color = XSSFFont.DEFAULT_FONT_COLOR;
normalStyle.SetFont(font);
cell.CellStyle = normalStyle;
}
但是,满足条件时字体不会改变。似乎该样式适用于所有单元格,而不是我在循环中得到的单元格。我已经阅读了一些与此问题相关的问题,但我无法使其正常工作。
这项新尝试正在格式化所有单元格。不管是否满足条件
ICellStyle redStyle = cell.CellStyle;
font.Color = IndexedColors.Red.Index;
redStyle.SetFont(font);
//This is how I am trying to change cells format
if (integrity < 1)
{
cell.CellStyle.SetFont(font);
}
Joao 响应将使用“normalStyle”格式化所有单元格
【问题讨论】: