0
  • 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 ...."??

amartin
  • 330
  • 2
  • 4
  • 17
  • 1
    Look at this https://stackoverflow.com/questions/17281105/asp-net-mvc-returning-an-xls-file-inside-an-zip-file – FakeisMe Sep 01 '17 at 07:18
  • I'm not sure what to do with that example. I'm adding multiple files, so I need to use `zipFile.AddEntry("File Name", fileContent)` multiple times before calling `zipFile.Save(stream);` and returning the zip file to the client.. – amartin Sep 02 '17 at 01:46
  • the using statement will end the zip OBJECT once the second using execution is done.Please check the code again. zip outside the zip 'using block' using doesn't throw error it just ends . Check the code. – FakeisMe Sep 05 '17 at 10:36
  • @FakeisMe, thanks for catching that. That was my typo when editing the code to post for this question. Should be right now. It was correct in my actual code. – amartin Sep 06 '17 at 02:24
  • Edited to include a foreach loop showing that I'm building multiple spreadsheets that get added to the zip file. – amartin Sep 06 '17 at 02:32
  • This is still an open problem that I'm trying to resolve. – amartin Sep 10 '17 at 19:40

0 Answers0