您的位置:首页 > 编程语言 > ASP

解决把水晶报表下载到客户端过程中,出现下载整个页面(.aspx)或者直接载web页中打开word文件的问题!

2004-11-23 09:28 1121 查看
给一段代码,研究一下,希望对遇到这样问题的朋友有所帮助。
这里特别感谢:浩子的无私帮助!
http://www.cnblogs.com/haozidong

private void btnOutForWord_Click( object sender, System.EventArgs e )
                    if ( Session["reportname"] != null && Session[Session["reportname"].ToString()] != null )
                            ReportDocument oRpt = new ReportDocument();
                oRpt = ( ReportDocument )Session[Session["reportname"].ToString()];
                crvMain.ReportSource = oRpt;    

                //设置导出选项。
                ExportOptions myExp = new ExportOptions();

                myExp.ExportFormatType = ExportFormatType.WordForWindows;        //导出到Word格式
                //myExp.ExportFormatType = ExportFormatType.Excel;                //导出到Excel格式
        
                //myExp.ExportFormatType = ExportFormatType.PortableDocFormat;    //导出到Pdf格式

                //定义导出内容
                ExportRequestContext myExpReq = new ExportRequestContext();
                myExpReq.ExportInfo = myExp;

                //产生报表Stream
                Stream myReportStream;
                myReportStream = oRpt.FormatEngine.ExportToStream(myExpReq);
                Response.ClearHeaders();
                Response.ClearContent();

                Response.ContentType = "application/msword";
                Response.AddHeader("Content-disposition","attachment; filename=" + DateTime.Now.ToString("yyyyMMddhhssmm") + ".doc");

                //Response.ContentType = "application/vnd.ms-excel";
                //Response.AddHeader("Content-disposition","attachment; filename=" + year.ToString() + "." + month.ToString() + ".xls");

                //Response.ContentType = "application/pdf";
                //Response.AddHeader("Content-disposition","attachment; filename=" + year.ToString() + "." + month.ToString() + ".pdf");

                byte[] myReportByte = new byte[myReportStream.Length];
                myReportStream.Read( myReportByte,0,(int)myReportStream.Length );
                Response.BinaryWrite( myReportByte );
                Response.Flush();
                Response.End();
            }
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: