为什么EPPlus在设置PatternType时告诉我“当patterntype没有设置时不能设置颜色”?
我有这个代码来尝试设置标题行的样式:
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 was unhandled。。.Message =不能设置颜色,当没有设置patterntype。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(除了使用的颜色以及应用代码的单元格区域)完全相同。
仔细看两条线:
worksheet.Cells["A32:D32"].Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Cells["A32:D33"].Style.Fill.BackgroundColor.SetColor(Color.CornflowerBlue);
第二行有D33,而不是D32,所以如果D33没有设置,它会抛出这个错误。