Why does EPPlus tell me that I cannot set the color if patterntype is not set when I installed PatternType?

I have this code to try to create a title bar:

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); 

It does not work in the last line above with "System.ArgumentException was unhandled..Message = Unable to set color if patterntype is not set. Source = EPPlus.,."

What could be the real problem? I do what I do, I'm wrong, right?

For more context:

 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); 

Please note that before adding the style header line, I used the β€œstyle for the rest” code and did not encounter this problem. The code is exactly the same as for setting PatternType and then BackgroundColor (except for the color used and the range of cells to which the code applies).

+7
c # excel epplus epplus-4
source share
1 answer

Look carefully at the two lines:

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

The second line is D33 instead of D32 , so if D33 is not already installed, this will lead to an error.

+11
source share

All Articles