HOY
HOY

Reputation: 1007

EPPlus error after image added, Value cannot be null, Parameter name: contentType

I have created an excel file using EPPlus and everything is OK until I try to add an image to my worksheet and I receive the error

(Value cannot be null, Parameter name: contentType)

in the following line:

package.Workbook.Worksheets.Add("MasterPackingList", createPackingListExcel(dt));

When below code part is removed, it works

System.Drawing.Image logo = System.Drawing.Image.FromFile(Server.MapPath("~/Images/")+"PLLogo_Nestle.png");
var addedLogo = worksheet.Drawings.AddPicture("Logo", logo);

Below is how I provide the excel to users:

Response.Clear();
Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(package.GetAsByteArray());
Response.End();

Upvotes: 0

Views: 1275

Answers (1)

HOY
HOY

Reputation: 1007

Everything is correct but when worksheet is returned by a method (createPackingListExcel in my case) this misterious error occurs.

So instead of returning the worksheet, I send my package to the function as parameter and let the worksheet to be added to the package within the function createPackingListExcel, then error is gone. Interesting...

Upvotes: 1

Related Questions