如何使用Open XML下载Excel文件之前?

我正在使用下面的代码来创build,它会向用户提示用户是否能够保存或打开或取消Excel文件…..

我成功地能够下载文件,但我需要压缩之前,它是显示用户提示,后来的zip文件将显示给用户喜欢与选项打开或保存或取消…..

我怎样才能不使用任何其他第三方库,并使用微软自己的Gzip DLL?

下面的代码是出口到Excelfunction:

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); } 

任何人都可以请帮助这个如何压缩文件之前,它显示给用户?

提前谢谢了…..

修改代码:

  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); } 

检查SharpZipLib库 。 它工作得很好,即使在商业应用中也可以自由使用。

您可以使用JCraft的JZlib。 很容易使用,压缩声明可以看起来像这样,代码里面取决于你在做什么,但你可以find工作的例子在JZlib的例子:

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