This project has moved. For the latest updates, please go here.

How to Export Excel File as a Zip File

Jan 10, 2014 at 9:44 PM
I just wanted to know if it's possible to export a Zip package that contains an Excel file created by ClosedXML.

I've been able to successfully export my data to just Excel using your documentation page titled "How do I deliver an Excel file in ASP.NET."

However I am trying to package my Excel file with a PowerPoint file into a Zip package. Unfortunately my Excel file in the zip package has an error message when I try to open it that says "Excel cannot open the file 'NewPresentation.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file." The PowerPoint file was able to export fine.

I've tried 2 separate functions and was unsuccessful in both. Any help would be greatly appreciated. Thank you.

Steve
public void StreamZipNEW(Dictionary<int, MemoryStream> reportStreams, ExcelClosedXML excelClosedXML, string destPPTXFile, string destXLSXFile, string fileName)
        {
            MemoryStream outputStream = new MemoryStream();

            reportStreams[0].Position = 0; //PowerPoint
            reportStreams[1] = new MemoryStream(); //Excel
            reportStreams[1].Position = 0;

            Telerik.Web.Zip.ZipPackage zipPackage = Telerik.Web.Zip.ZipPackage.Create(outputStream);

            zipPackage.AddStream(reportStreams[0], destPPTXFile);
            zipPackage.AddStream(reportStreams[1], destXLSXFile);

            string destFile = string.Format("{0}.zip", fileName);
            zipPackage.Close(false);
            outputStream.Position = 0;
            if (outputStream != null && outputStream.Length > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename=\"" + destFile);
                Response.ContentType = "application/zip";
                Response.BufferOutput = false;   // to prevent buffering
                byte[] buffer = new byte[1024];
                int bytesRead = 0;

                excelClosedXML.XLWorkbook.SaveAs(reportStreams[1]);

                while ((bytesRead = outputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Response.OutputStream.Write(buffer, 0, bytesRead);
                }

                Response.End();
            }
            outputStream.Close();

        }
public void StreamZipNEW2(Dictionary<int, MemoryStream> reportStreams, ExcelClosedXML excelClosedXML, string destPPTXFile, string destXLSXFile, string fileName)
        {
            MemoryStream outputStream = new MemoryStream();
            string destFile = string.Format("{0}.zip", fileName);

            HttpResponse httpResponse = Response;
            httpResponse.Clear();

            Telerik.Web.Zip.ZipPackage zipPackage = Telerik.Web.Zip.ZipPackage.Create(outputStream);

            excelClosedXML.XLWorkbook.SaveAs(reportStreams[1]);

            zipPackage.AddStream(reportStreams[0], destPPTXFile); //PowerPoint
            zipPackage.AddStream(reportStreams[1], destXLSXFile); //Excel

            zipPackage.Close(false);
            
            httpResponse.ContentType = "application/zip";
            httpResponse.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}\"", destFile));
            outputStream.WriteTo(httpResponse.OutputStream);
            outputStream.Close();

            httpResponse.End();
        }