我有这段代码来尝试样式化标题行:
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完全相同(除了使用的颜色和应用代码的单元格范围)。