2016-07-27 49 views
1

Ich erstelle eine Excel-Datei mit EPPlus-Bibliothek und ich möchte es auf den Client herunterladen. HierEPPlus mit Silverlight

ist ein Beispiel für EPPlus

ExcelPackage pck = new ExcelPackage(); 
var ws = pck.Workbook.Worksheets.Add("Sample1"); 

ws.Cells["A1"].Value = "Sample 1"; 
ws.Cells["A1"].Style.Font.Bold = true; 
var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect); 
shape.SetPosition(50, 200); 
shape.SetSize(200, 100); 
shape.Text = "Sample 1 saves to the Response.OutputStream"; 

pck.SaveAs(HttpContext.Current.Response.OutputStream); 
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Sample1.xlsx"); 

Dieser Code xls-Datei erstellt und ermöglicht es Client von einer aspx Seite zum Download bereit. Ich habe versucht, XLS-Datei mit "HttpWebRequest" -Klasse von my.aspx Seite anfordern, aber ich bekomme einen Fehler, der sagt "Operation ist nicht gültig aufgrund des aktuellen Status des Objekts".

Wie kann ich diese XLS-Datei vom Server auf den Client herunterladen?

Antwort

1

Wahrscheinlich besser, eine BinaryWrite zu tun. Hier ist, wie ich es mache:

HttpContext.Current.Response.Clear(); 
HttpContext.Current.Response.Buffer = true; 
HttpContext.Current.Response.Charset = ""; 
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=myfilename.xlsx"); 
HttpContext.Current.Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); 
HttpContext.Current.Response.BinaryWrite(pck.GetAsByteArray()); 
HttpContext.Current.Response.Flush();