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