Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带Silverlight的EPPlus_C#_Excel_Silverlight_Epplus - Fatal编程技术网

C# 带Silverlight的EPPlus

C# 带Silverlight的EPPlus,c#,excel,silverlight,epplus,C#,Excel,Silverlight,Epplus,我正在用EPPlus库创建一个excel文件,我想将其下载到客户端 下面是一个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("Shape

我正在用EPPlus库创建一个excel文件,我想将其下载到客户端

下面是一个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");
此代码创建xls文件,并允许从aspx页面将其下载到客户端。 我试图从my.aspx页面请求具有“HttpWebRequest”类的xls文件,但我收到一个错误,该错误表示“由于对象的当前状态,操作无效”


如何将此xls文件从服务器下载到客户端

可能最好使用
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();