Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

New Post: Create zip of workbooks and download

$
0
0
I'm trying to create several workbooks in memory and putting them into a zip file that will be returned to the browser for download. Combining the example here to deliver an XLWorkbook for download and using ZipArchive functionality in the newest versions of ASP, here's a snippet of what I've done:

using (var ZipStream = new MemoryStream())
            {
                using (var Archive = new ZipArchive(ZipStream, ZipArchiveMode.Create, true))
                {
                    for (int i = 0; i < ExperimentList.Count(); i++)
                    {

                        WorkbookFileName = "Experiment_" + ExperimentList[i].ExperimentID.ToString() + ".xlsx";
                        dt = GetData(ExperimentList[i].ExperimentID);
                        Workbook = GenerateExcelDoc(dt, WorkbookFileName, SheetName);

                        var File = Archive.CreateEntry(WorkbookFileName);

                        using (var entryStream = File.Open())
                        using (var streamWriter = new StreamWriter(entryStream))
                        {
                            Workbook.SaveAs(ZipStream);
                            ZipStream.Seek(0, SeekOrigin.Begin);
                        }
                    }
                }

                // create HTTP Response
                HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
                httpResponse.Clear();
                httpResponse.ContentType = "application/zip";
                httpResponse.AddHeader("content-disposition", "attachment; filename=" + ZipFileName);

                // deliver zip file from memory stream to browser
                //ZipStream.Seek(0, SeekOrigin.Begin);
                ZipStream.WriteTo(httpResponse.OutputStream);
                ZipStream.Seek(0, SeekOrigin.Begin);
                ZipStream.Close();
                httpResponse.End();
            }
When I open the zip file, I don't see any workbooks. Instead I get what I believe are components to make an XLWorkbook. Here's the top directory in the zip:
  • _rels/
  • docProps/
  • package/
  • xl/
    [Content_Types].xml
No matter how many files I attempt to zip, it's just this basic directory structure (with more files in each folder). The total zip file always comes out about 17 kb.

I've also tried other variations in my code like using

streamWriter.Write(workbook);

instead of

Workbook.SaveAs(ZipStream);

and I end up getting a zip file w/ all the workbooks I wanted. But I can't open any of the workbooks and instead I get a 'file corrupted' message.

Has anyone else been able to write and zip several Excel docs in memory and delivered directly to the browser for download?

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>