简体   繁体   中英

How to zip Excel file before it is downloading using Open XML?

I am using below code to create and it will show user prompt to user whether the user can able to save or open or cancel a excel file.....

I am successfully able to download the file but I need to zip before it is showing user prompt, Later zip file will be showed to the user like with options open or save or cancel.....

How can I do that with not using any other third party library and using Microsoft own Gzip DLL?

The below code is for exporting to excel functionality:

public ActionResult ExportToExcel()
{

    byte[] file;
    string targetFilename = string.Format("{0}-{1}.xlsx", "Generated", "excel");

    DataTable dt = common.CreateExcelFile.ListToDataTable(GetSearchDraftPRResults());
    common.CreateExcelFile excelFileForExport = new CreateExcelFile();
    file = excelFileForExport.CreateExcelDocumentAsStream(dt, targetFilename);
    Response.Buffer = true;
    return File(file, "application/vnd.ms-excel", targetFilename);          
}

Would anyone please help on this how to zip a file before it is showing to user?

Many thanks in advance.....

Modified Code:

    public ActionResult ExportToExcel()
    {

        byte[] file;
        string targetFilename = string.Format("{0}-{1}.xlsx", "Generated", "excel");

        DataTable dt = common.CreateExcelFile.ListToDataTable(GetSearchDraftPRResults());
        common.CreateExcelFile excelFileForExport = new CreateExcelFile();
        file = excelFileForExport.CreateExcelDocumentAsStream(dt, targetFilename);
        Response.Buffer = true;
        byte[] zipFile = Compress(file);
        return File(file, "application/vnd.ms-excel", targetFilename);          
    }


    public byte[] Compress(FileInfo fileToCompress)
    {
        using (FileStream originalFileStream = fileToCompress.OpenRead())
        {
            if ((System.IO.File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
            {
                using (FileStream compressedFileStream = System.IO.File.Create(fileToCompress.FullName + ".gz"))
                {
                    using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                    {
                        originalFileStream.CopyTo(compressionStream);

                    }

                }

            }

            MemoryStream mem = new MemoryStream();
            CopyStream(originalFileStream, mem);
            return mem.ToArray();
        }

    }


    public static void CopyStream(Stream input, Stream output)
    {
        byte[] b = new byte[32768];
        int r;
        while ((r = input.Read(b, 0, b.Length)) > 0)
            output.Write(b, 0, r);
    }

Check out the SharpZipLib library . It works very well and is free to use even in commercial applications.

You can use JZlib from JCraft. Very easy to use, compression declaration can look like this, the code inside depends on what's you doing but you can find working example in JZlib examples:

public byte[] compress(byte[] buf, int start, int[] len) {
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM