Question Excel byte array to stream writer zip file

alketrazz

New member
Joined
Jan 24, 2018
Messages
3
Programming Experience
Beginner
Hi

This is my first post here, so hope everything makes sense.

I have the following code that works fine. I create an excel file using ClosedXML. I then use the MemoryStream to return a byte array.
I then return a FileResult specifying the type.

C#:
var bytes = repo.DownloadRotas(code, wcDate);
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "rotas.xlsx");

What I want to do is do this for multiple files and I believe the only way is by creating a zip file.

This is the code I have.

C#:
using (var ms = new MemoryStream())
            {
                using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
                {
                    // loop through each person
                    foreach (var code in codes)
                    {
                        // CREATE AN ENTRY WITH THE XLSX FILENAME
                        var filename = string.Format("Rotas - {0}.xlsx", code);


                        // GET THE BYTES FOR THE FILE FROM YOUR DOWNLOADROTAS METHOD
                        var bytes = repo.DownloadRotas(code, wcDate);
                        var demoFile = archive.CreateEntry(filename);


                        using (var entryStream = demoFile.Open())
                        using (var streamWriter = new StreamWriter(entryStream))
                        {
                            // WRITE BYTES IN HERE
                            streamWriter.Write(bytes);
                        }
                    }
                }
                return File(ms.ToArray(), "application/zip", "test.zip");
            }

This creates a zip file with the excel files inside, but the files are corrupt. I get the following error.

Excel cannot open the file.... becase the file format or file extension is not valid .....

It seems like the issue is that I'm not specifying what the files are like in the code where I return a single fileresult file.

Really hopeful that this can be done.

Thank you in advance
 
You should not be using a StreamWriter at all. A StreamWriter is specifically for writing text to a Stream. You're not writing text. Just call the Write method of your Stream, i.e. 'entryStream.Write'.
 
Excel byte array to writer zip file

Hi

Thank you . That has done it. I had to do this which i think is right. add the 0 offset and the count of bytes?

C#:
using (var entryStream = demoFile.Open())
    {
        //WRITE BYTES IN HERE 
        entryStream.Write(bytes, 0, bytes.Count());
    }

This does work, but for a lot of the files it is return the error

We found a problem with some content..... Do you want us to try and recover...

Repaired Records: Cell information from /xl/worksheets/sheet.xml part

I guess there must be a problem with how I'm building up the file using closed xml but it doesn't error when I download one file.
 

Latest posts

Back
Top Bottom