您当前的位置: 首页 > 

[水晶报表]导出PDF or Excel,并返回文件到用户的IE浏览器中

发布时间:2012-07-10 12:37:13 ,浏览量:0

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CrystalDecisions.Shared; using CrystalDecisions.CrystalReports.Engine; namespace MinKe.Library {      //=========================================================      // File: WebForm1.aspx      // Version:0.0      // Date: 2003-1-15      // Script Written by satan      //=========================================================      // Copyright (C) 2001,2002 www.LionSky.Net. All rights reserved.      // Web: http://www.Lionsky.net,http://www.Lionsky.net/MyWebSite/Index.Aspx      // Email: lion-a@sohu.com      //=========================================================      ///      /// WebForm1 的摘要说明。      ///      public class WebForm1 : System.Web.UI.Page      {          protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;          private CrystalDecisions.CrystalReports.Engine.ReportDocument ReportDoc;          private TableLogOnInfo logOnInfo;          private DiskFileDestinationOptions FileOPS;          protected System.Web.UI.WebControls.Button Button1;          private ExportOptions ExOPS;          public WebForm1()          {               //               // TODO: 在此处添加构造函数逻辑               //               ReportDoc=new ReportDocument();               logOnInfo=new TableLogOnInfo();               FileOPS=new DiskFileDestinationOptions();          }          ///          /// 导出报表文件为PDF格式          ///          ///报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数           ///报表文件所使用的数据源,是一个Dataset          ///你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的          ///bool成功返回true,失败返回false          public bool ExportToPDF(string ReportFile,object ReportDataSource,string PDFFileName)          {               try               {                    ReportDoc.Load(ReportFile);                    ReportDoc.SetDataSource(ReportDataSource);                    FileOPS.DiskFileName=PDFFileName;                    ExOPS=ReportDoc.ExportOptions;                    ExOPS.DestinationOptions=FileOPS;                    ExOPS.ExportDestinationType=CrystalDecisions.Shared.ExportDestinationType.DiskFile;                    ExOPS.ExportFormatType=CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;                    ReportDoc.Export();                    return true;               }               catch               {                    return false;               }          }          ///          /// 导出报表文件到Excel格式          ///          ///报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数          ///报表文件所使用的数据源,是一个Dataset          ///你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的          ///成功返回true失败返回false          public bool ExportToExcel(string ReportFile,object ReportDataSource,string ExcelFileName)          {               try               {                    ReportDoc.Load(ReportFile);                    ReportDoc.SetDataSource(ReportDataSource);                    FileOPS.DiskFileName=ExcelFileName;                    ExOPS=ReportDoc.ExportOptions;                    ExOPS.DestinationOptions=FileOPS;                    ExOPS.ExportDestinationType=CrystalDecisions.Shared.ExportDestinationType.DiskFile;                    ExOPS.ExportFormatType=CrystalDecisions.Shared.ExportFormatType.Excel;                    ReportDoc.Export();                    return true;               }               catch               {                    return false;               }          }          ///          /// 返回PDF文件到用户的IE浏览器中          ///          ///报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数          ///报表文件所使用的数据源,是一个Dataset          ///用于显示PDF WebForm          ///          public bool ReturnPDF(string ReportFile,object ReportDataSource,System.Web.UI.Page page)          {               int temp;               temp=System.Convert.ToInt32(System.DateTime.Now.Millisecond.ToString());               System.Random ra=new System.Random(temp);               int TmpNumber=ra.Next();               string TmpPDFFileName="c://"+System.Convert.ToString(TmpNumber)+".pdf";               if (ExportToPDF(ReportFile,ReportDataSource,TmpPDFFileName)==true)               {                    page.Response.ClearContent();                    page.Response.ClearHeaders();                    page.Response.ContentType="application/pdf";                    page.Response.WriteFile(TmpPDFFileName);                    page.Response.Flush();                    page.Response.Close();                    System.IO.File.Delete(TmpPDFFileName);                    return true;               }               else               {                    return false;               }          }          ///          /// 返回Excel文件到用户的IE浏览器中          ///          ///          ///          ///          ///          public bool ReturnExcel(string ReportFile,object ReportDataSource,System.Web.UI.Page page)          {               int temp;               temp=System.Convert.ToInt32(System.DateTime.Now.Millisecond.ToString());               System.Random ra=new System.Random(temp);               int TmpNumber=ra.Next();               string TmpExcelFileName="c://"+System.Convert.ToString(TmpNumber)+".xls";               if (ExportToExcel(ReportFile,ReportDataSource,TmpExcelFileName)==true)               {                    page.Response.ClearContent();                    page.Response.ClearHeaders();                    page.Response.ContentType="application/xls";                    page.Response.WriteFile(TmpExcelFileName);                    page.Response.Flush();                    page.Response.Close();                    System.IO.File.Delete(TmpExcelFileName);                    return true;               }               else               {                    return false;               }          }          private void Page_Load(object sender, System.EventArgs e)          {          }          #region Web Form Designer generated code          override protected void OnInit(EventArgs e)          {               //               // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。               //               InitializeComponent();               base.OnInit(e);          }          ///          /// 设计器支持所需的方法 - 不要使用代码编辑器修改          /// 此方法的内容。          ///          private void InitializeComponent()          {                  this.Button1.Click += new System.EventHandler(this.Button1_Click);               this.Load += new System.EventHandler(this.Page_Load);          }          #endregion                  private void Button1_Click(object sender, System.EventArgs e)          {               MinKe.Data.Db list = new MinKe.Data.Db();               //此处生成PDF文件,list.GetView方法返回一个查询后的DataView数据集               ExportToPDF(Server.MapPath("Report1.rpt"),list.GetView("Select G_B_ID,G_B_Class,G_B_Name,G_B_Sex,G_B_Address,G_B_A_Date,G_B_D_Date From G_Bury Where G_B_ID<200","aa").Table,"c://test.pdf");               //此处返回PDF文件,到客户端的IE中,客户端必须要安装Acrobat才可浏览               ReturnPDF(Server.MapPath("Report1.rpt"),list.GetView("Select G_B_ID,G_B_Class,G_B_Name,G_B_Sex,G_B_Address,G_B_A_Date,G_B_D_Date From G_Bury Where G_B_ID<200","aa").Table,this);               //------生成Excel并返回到IE中的方法和上面相同,分别调用ExportToExcel和ReturnExcel方法--------          }             } }

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    109362博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.3513s