2016-07-27 11 views
1

私はEPPlusライブラリを使用してExcelファイルを作成しています。クライアントにダウンロードします。ここでEPPlus with Silverlight

は、このコードは、XLSファイルを作成し、aspxページからクライアントにダウンロードすることができます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"); 

の一例です。 my.aspxページから 'HttpWebRequest'クラスのxlsファイルを要求しようとしましたが、 '操作がオブジェクトの現在の状態のため有効ではありません'というエラーが表示されます。

このxlsファイルをサーバーからクライアントにダウンロードするにはどうすればよいですか?

答えて

1

BinaryWriteの方がよいでしょう。ここで私はそれを行う方法です:

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