This is a migrated thread and some comments may be shown as answers.

Export zipped excel file - Looking for best practice

7 Answers 180 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 12 Jan 2021, 09:55 AM

Hi,

I would like to export some excel files in a zip without writing on disk.

I have seen ZipLibrary example and I would like to adapt it with excel (if possible).

The idea is to do something like this in "OnClick" event for exemple (see code bellow)

Thanks in advance for your help

using (MemoryStream stream = new MemoryStream())
{
    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: null))
    {
        //I don't need TXT but just for testing ...
        using (ZipArchiveEntry entry = archive.CreateEntry("text1.txt"))
        {
            StreamWriter writer = new StreamWriter(entry.Open());
            writer.WriteLine("Hello world!");
            writer.Flush();
        }
 
        //Adding excel file
        using (ZipArchiveEntry entry = archive.CreateEntry("testxlsx.xlsx"))
        {
            //How to handle / edit XLSX file here ?
        }
    }
 
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=zipfile.zip");
    Response.ContentType = "application/zip";
    Response.BinaryWrite(stream.ToArray());
    Response.End();
}

7 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 12 Jan 2021, 11:20 AM

Hello Julien,

I believe that you can use the approach described here: Create an archive from a list of files.

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Julien
Top achievements
Rank 1
answered on 12 Jan 2021, 11:25 AM

Hi Dimitar,

Thanks for your answer.

As i said in my first post, il would like to do it without writing file on disk.

Do you mean it's impossible to do that ?

Regards,

0
Accepted
Dimitar
Telerik team
answered on 12 Jan 2021, 01:26 PM

Hi Julien,

No, the example uses FileStreams that can be replaced with MemoryStreams for example (for both the zip and the XLSX file). 

Please let me know if there is anything else I can help you with. 

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Julien
Top achievements
Rank 1
answered on 12 Jan 2021, 01:43 PM

Hi again,

Thx, i will try this and come back if necessary :-)

Regards,

0
Julien
Top achievements
Rank 1
answered on 12 Jan 2021, 03:53 PM

Hi Again,

I have replaced FileStream by MemoryStream as you suggested and it runs without error ! Thanks !

The new issue I have now is I can't read the file, Excel says "format or extention is not correct, check if file is not damaged ..."

Is it something regarding compression ?

Find new code bellow

Regards,

using (MemoryStream stream = new MemoryStream())
{
    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: null))
    {
        //I don't need TXT but just for testing ...
        using (ZipArchiveEntry entry = archive.CreateEntry("text1.txt"))
        {
            StreamWriter writer = new StreamWriter(entry.Open());
            writer.WriteLine("Hello world!");
            writer.Flush();
        }
 
        //Adding excel file
        using (ZipArchiveEntry entry = archive.CreateEntry("testxlsx.xlsx"))
        {                                                 
            var entryStream = entry.Open();
            MemoryStream ms = new MemoryStream();
 
            using (IWorkbookExporter workbook = SpreadExporter.CreateWorkbookExporter(SpreadDocumentFormat.Xlsx, ms))
            {
                using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("SHEET_TEST"))
                {
                    worksheet.SkipRows(1);
                    using (IRowExporter row = worksheet.CreateRowExporter())
                    {
                        row.SkipCells(1);
                        using (ICellExporter cell = row.CreateCellExporter())
                        {
                            cell.SetValue("TEST CELL VALUE");
                        }
                    }
                }
            }
 
            ms.CopyTo(entryStream);
            entryStream.Flush();
 
            ms.Close();
            ms.Dispose();
        }
    }
 
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=zipfile.zip");
    Response.ContentType = "application/zip";
    Response.BinaryWrite(stream.ToArray());
    Response.End();
}
0
Dimitar
Telerik team
answered on 13 Jan 2021, 10:44 AM

Hi Julien,

Your approach is correct. One more thing that you need to do is reset the stream position before calling the CopyTo method: 

ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(entryStream);

Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Julien
Top achievements
Rank 1
answered on 13 Jan 2021, 01:59 PM

Hi,

It works fine, thanks again for your help !

Regards,

Tags
ZipLibrary
Asked by
Julien
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Julien
Top achievements
Rank 1
Share this question
or