2016-07-29 23 views
7

Tôi đã có mã này để cố gắng phong cách một dòng tiêu đề:..Tại sao EPPlus lại nói với tôi rằng tôi "Không thể thiết lập màu khi không có thiết lập patterntype" khi tôi đã đặt 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); 

Nó không thành công trên dòng cuối cùng trên với "System.ArgumentException là unhandled .Message = Không thể thiết lập màu sắc khi patterntype Nguồn = EPPlus ... "

Vấn đề thực sự có thể là gì? Tôi am thực hiện những gì nó không đúng, đúng không?

Đối với bối cảnh nhiều hơn:

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

Lưu ý rằng tôi đã có "phong cách với phần còn lại" mã trước khi thêm "dòng tiêu đề kiểu" và không chạy vào vấn đề này. Mã này chính xác giống như thiết lập PatternType và sau đó là BackgroundColor (ngoại trừ màu được sử dụng và phạm vi ô được áp dụng cho).

+0

Tôi đã bỏ lỡ dòng này: worksheet.Cells ["A32: D32"]. Style.Fill.PatternType = ExcelFillStyle.Solid; – Kadaj

Trả lời

11

Nhìn kỹ vào hai dòng:

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

Dòng thứ hai có D33 thay vì D32 vì vậy nếu D33 không được thiết lập nhưng nó sẽ ném lỗi đó.

Các vấn đề liên quan