In my code I query border style for multiple times, and the first time the result is incorrect, but the 2nd iteration gets it right. It seems like a bug to me. Please check the attached file and the sample code that reproduces the issue:
public class InconsistentCellStyleIssue :IssueTestBase
{
[FactDebugOnly]
public void Test()
{
var workbook = new Workbook(GetFullPath(“sample.xlsx”));
var styles = Enumerable.Empty().Select(s => new {Name = s, Style = s}).ToList();
var range = workbook.Worksheets[“ByCostCenter”].Cells.MaxDisplayRange;
var cells = range.Worksheet.Cells;
for (var i = 0; i < 2; i++) // 2 iterations
{
for (var row = range.FirstRow; row < range.FirstRow + range.RowCount; row++)
{
if (cells.Rows[row].IsHidden) continue;
for (var col = range.FirstColumn; col < range.FirstColumn + range.ColumnCount; col++)
{
if (cells.Columns[col].IsHidden) continue;
var cell = cells[row, col];
var style = GetCellStyle(cell);
var cellStyle = Map(style);
styles.Add(new {cell.Name, Style = cellStyle});
}
}
}
var issues = styles.Distinct()
.GroupBy(s => s.Name, s => s.Style)
.Where(g => g.Count() > 1)
.ToDictionary(g => g.Key, g => string.Join(" -> “, g));
issues.Should().BeEmpty(); // But contains{[A6, Thin, Thin -> Thick, Thin]}
// var l1 = GetCellStyle(workbook.Worksheets[“ByCostCenter”].Cells[“A6”]).Borders[BorderType.BottomBorder].LineStyle;
// var l2 = GetCellStyle(workbook.Worksheets[“ByCostCenter”].Cells[“A6”]).Borders[BorderType.BottomBorder].LineStyle;
// l1.Should().Be(l2);
}
private static Style GetCellStyle(Cell cell)
{
var style = cell.GetDisplayStyle();
if (style.HorizontalAlignment == TextAlignmentType.General &&
(cell.Type == CellValueType.IsNumeric || cell.Type == CellValueType.IsDateTime))
style.HorizontalAlignment = TextAlignmentType.Right;
return style;
}
private static string Map(Style style)
{
return string.Join(”, “,
new[] {BorderType.BottomBorder, BorderType.TopBorder}.Select(
bt => style.Borders[bt].LineStyle.ToString()));
}
}
var issues = styles.Distinct()
.GroupBy(s => s.Name, s => s.Style)
.Where(g => g.Count() > 1)
.ToDictionary(g => g.Key, g => string.Join(” -> “, g));
issues.Should().BeEmpty(); // But contains{[A6, Thin, Thin -> Thick, Thin]}
}
private static Style GetCellStyle(Cell cell)
{
var style = cell.GetDisplayStyle();
if (style.HorizontalAlignment == TextAlignmentType.General &&
(cell.Type == CellValueType.IsNumeric || cell.Type == CellValueType.IsDateTime))
style.HorizontalAlignment = TextAlignmentType.Right;
return style;
}
private static string Map(Style style)
{
return string.Join(”, ",
new[] {BorderType.BottomBorder, BorderType.TopBorder}.Select(
bt => style.Borders[bt].LineStyle.ToString()));
}
}