简体   繁体   English

在使用Open XML下载之前,如何压缩Excel文件?

[英]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..... 我正在使用下面的代码创建,它将向用户显示用户提示,用户是否可以保存或打开或取消Excel文件。

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..... 我可以成功下载文件,但需要先压缩才能显示用户提示,以后会像打开选项,保存或取消选项一样向用户显示zip文件。

How can I do that with not using any other third party library and using Microsoft own Gzip DLL? 不使用任何其他第三方库并使用Microsoft自己的Gzip DLL怎么办?

The below code is for exporting to excel functionality: 以下代码用于导出到excel功能:

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 . 查看SharpZipLib库 It works very well and is free to use even in commercial applications. 它效果很好,即使在商业应用中也可以免费使用。

You can use JZlib from JCraft. 您可以从JCraft使用JZlib。 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: 压缩声明非常易于使用,看起来像这样,其中的代码取决于您在做什么,但是您可以在JZlib示例中找到有效的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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