Issue with Returning Epplus spreadsheets with an Image in .zip file using DotNetZip

246 views Asked by At
  • This scenario works fine without any images on the spreadsheet, but after attempting to add an image to the spreadsheets that get put in the zip file, the spreadsheets open with the excel error of "We found a problem with some content ....".
  • I have other methods using Epplus without DotNetZip that use the exact same code to insert the image into a spreadsheet and they work fine with no errors or issues.

Code that works to return a single spreadsheet with an image

public async Task<ActionResult> GenerateSpreadsheet(ReportViewModel reportViewModel)
{
  using (var excelPackage = new ExcelPackage())
  {
    Bitmap logoFile = getLogoFile();

    var companyLogo = worksheet.Drawings.AddPicture("File Name", logoFile);
    companyLogo.From.Column = columnIndex - 4;
    companyLogo.From.Row = rowIndex;
    companyLogo.SetSize(logoFile.Width, logoFile.Height);

    //Write all the stuff to the spreadsheet

    Response.ClearContent();
    Response.BinaryWrite(excelPackage.GetAsByteArray());
    string fileName = "attachment;filename=Project_Report_Export.xlsx";
    Response.AddHeader("content-disposition", fileName);
    Response.ContentType = "application/excel";
    Response.Flush();
    Response.End();
  }
}

Code that will build a spreadsheet, add it to a zip file, but where the spreadsheet will open with the "We found a problem with some content ...." if an image was added to the spreadsheet as shown below. If there is no image added to it, it will open without the error.

public async Task<ActionResult> GenerateSpreadsheet(ReportViewModel reportViewModel)
{
  using (var stream = new MemoryStream())
  {
    using (ZipFile zip = new ZipFile())
    {
      foreach(var spreadSheet in listOfStuffToBuildFrom)
      {

        using (var excelPackage = new ExcelPackage())
        {
          Bitmap logoFile = getLogoFile();

          var companyLogo = worksheet.Drawings.AddPicture("File Name", logoFile);
          companyLogo.From.Column = columnIndex - 4;
          companyLogo.From.Row = rowIndex;
          companyLogo.SetSize(logoFile.Width, logoFile.Height);

          //Write all the stuff to the spreadsheet

          //Add the workbook to the zip file
          zip.AddEntry(excelPackage.Workbook.Properties.Title, excelPackage.GetAsByteArray());
        }
      }

      zip.Save(stream);
      return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Zip, "Project Reports.zip");
    }
  }
}

Why does the second method return spreadsheets that open with the error "We found a problem with some content ...."??

0

There are 0 answers