C# 动态创建和压缩Excel文件

C# 动态创建和压缩Excel文件,c#,asp.net,.net,asp.net-mvc,sharpziplib,C#,Asp.net,.net,Asp.net Mvc,Sharpziplib,我试图压缩一个Excel文件,并在提示中向用户显示压缩后的文件,其中包含保存、打开和取消选项 我正在使用sharpZip.Net库压缩Excel文件,当您单击要导出到Excel的链接时,用户提示无法显示 代码如下。我在这个方法中得到一个SingleGZipfile;,这表示该方法有一些无效参数 public ActionResult ExportToExcel() { byte[] file; string targetFilename = st

我试图压缩一个Excel文件,并在提示中向用户显示压缩后的文件,其中包含保存、打开和取消选项

我正在使用sharpZip.Net库压缩Excel文件,当您单击要导出到Excel的链接时,用户提示无法显示

代码如下。我在这个方法中得到一个SingleGZipfile;,这表示该方法有一些无效参数

    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;

       SingleGZip(file);
        //Stream memStream = new MemoryStream(file);

        //using (ZipFile zipFile = new ZipFile())
        //{
        //    zipFile.AddEntry("Generated-Excel.xlsx", "", memStream);
        //    Response.ClearContent();
        //    Response.ClearHeaders();
        //    Response.AppendHeader("content-disposition", "attachment; filename=Report.zip");

        //    zipFile.Save(Response.OutputStream);
        //}
        //return 

        //byte[] zipFile = Compress(file);
        return File(file, "application/vnd.ms-excel", targetFilename);          
    }

    private byte[] SingleGZip(string source)
    {
        string target = source + ".gz";

        using (Stream s = new GZipOutputStream(System.IO.File.Create(target)))
        {
            using (FileStream fs = System.IO.File.OpenRead(source))
            {
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
                s.Write(buffer, 0, buffer.Length);
            }

        }

    }
期望字符串作为参数,并且您在控制器中将比特数组传递给SigleZip方法

byte[] file; <-- array of bytes
        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;

       SingleGZip(file); <-- array of bytes

嗯,file是一个字节[],您的SingleGZip方法接受一个字符串。这正是编译器告诉您的。有什么不清楚的吗?您好,这是使用shapzip库压缩excel文件的正确方法吗?我还需要显示用户提示…我建议您应该使用shapzip在服务器上创建zip文件,然后返回到用户文件,或者仅使用“路径”查看,并在视图中使用“路径”创建下载链接
byte[] file; <-- array of bytes
        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;

       SingleGZip(file); <-- array of bytes