为什么当我设置了PatternType时,EPPlus告诉我“在未设置PatternType时无法设置颜色”?

33
我有这段代码来尝试样式化标题行:
worksheet.Cells["A32:D32"].Style.Font.Name = "Georgia";
worksheet.Cells["A32:D32"].Style.Font.Bold = true;
worksheet.Cells["A32:D32"].Style.Font.Size = 16;
worksheet.Cells["A32:D32"].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells["A32:D33"].Style.Fill.BackgroundColor.SetColor(Color.CornflowerBlue);

上面的最后一行代码出现了错误,报错信息为“System.ArgumentException未处理. . .Message=在未设置图案类型时无法设置颜色。 Source=EPPlus. . .

真正的问题可能是什么?我正在做它声称我没有做的事情吗?

更多背景信息:

worksheet.Cells["A32"].LoadFromCollection(bookDataList, true);
// style header row
worksheet.Cells["A32:D32"].Style.Font.Name = "Georgia";
worksheet.Cells["A32:D32"].Style.Font.Bold = true;
worksheet.Cells["A32:D32"].Style.Font.Size = 16;
worksheet.Cells["A32:D32"].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells["A32:D33"].Style.Fill.BackgroundColor.SetColor(Color.CornflowerBlue);
// style the rest
worksheet.Cells["A33:D59"].Style.Font.Name = "Candara";
worksheet.Cells["A33:D59"].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells["A33:D59"].Style.Fill.BackgroundColor.SetColor(Color.Cornsilk);
请注意,我在添加“样式标题行”之前已经使用了“样式其余部分”的代码,并且没有遇到这个问题。该代码与设置PatternType然后BackgroundColor完全相同(除了使用的颜色和应用代码的单元格范围)。

1
我漏掉了这一行代码:worksheet.Cells["A32:D32"].Style.Fill.PatternType = ExcelFillStyle.Solid; - Kadaj
1个回答

55

仔细看这两行:

worksheet.Cells["A32:D32"].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells["A32:D33"].Style.Fill.BackgroundColor.SetColor(Color.CornflowerBlue);

第二行中使用了D33而不是D32,因此如果D33尚未设置,它将抛出该错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接