Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/6 9:11:49
在系统中生成Excel流并传给用户_[Asp.Net教程]

在系统中生成Excel流并传给用户_[Asp.Net教程]

在系统中经常出现这样的情况就是生成excel流 并将生成的Excel 流传给用户

  一般的来讲可以利用 Response 来向客户端发送一个HTTP头 ,并将Excel流发送出去就可以了,但会出现一个问题就是点击打开的时候会出现两次 文件下载 对话框,主要的原因:(现在的猜测,还没有找到准确的原因)。


  第一次打开 时 ,在系统中已经存在了excel流的临时文件(就是文件,没有文件的后缀名的存在) ,但没有原来的(真实文件的存在),要求用户是否将临时文件保存到计算机其他的位置上。(是否跟没有后缀名有关)


  第二次 ,选择客户端文件的位置,是否保存到其他的非临时文件夹位置。


  现在的解决方案是


  新建一个页面专门提供导出数据的


  在这个页面中的Page_load中写这样的代码


private void Page_Load(object sender, System.EventArgs e)


{


// 在此处放置用户代码以初始化页面
ReportRule reportRule=new ReportRule();
byte[] result= reportRule.RenderReport(HttpContext.Current.Server.UrlDecode(this.Request.QueryString["peportpath"]),Request.QueryString["id"]); //生成excel流


HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";


HttpContext.Current.Response.AppendHeader("Content-disposition","attachment;filename=hahh.xls");


HttpContext.Current.Response.BinaryWrite(result);


HttpContext.Current.Response.End();


}







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759