[英]Creating and zipping Excel file on the fly
我正在嘗試壓縮Excel文件,並在提示中向用戶顯示生成的壓縮文件,並帶有保存,打開和取消的選項。
我正在使用SharpZip .Net庫壓縮Excel文件,並且當您單擊“ Export to excel
”鏈接時,用戶提示無法顯示。
代碼如下。 我在這種方法中得到了SingleGZip(file); ,表示該方法有一些無效的參數。
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);
}
}
}
private byte[] SingleGZip(string source) <-- give me a string
期望字符串作為參數,並且您在控制器中將叮咬數組傳遞給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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.