导航

给一段代码,研究一下,希望对遇到这样问题的朋友有所帮助。
这里特别感谢:浩子的无私帮助!
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();
            }

        }