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

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
     //=========================================================
     /// <summary>
     /// WebForm1 的摘要说明。
     /// </summary>
     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();
         }
         /// <summary>
         /// 导出报表文件为PDF格式
         /// </summary>
         /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
          /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
         /// <param name="PDFFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
         /// <returns>bool成功返回true,失败返回false</returns>
         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;
              }
         }
         /// <summary>
         /// 导出报表文件到Excel格式
         /// </summary>
         /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
         /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
         /// <param name="ExcelFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
         /// <returns>成功返回true失败返回false</returns>
         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;
              }
         }
         /// <summary>
         /// 返回PDF文件到用户的IE浏览器中
         /// </summary>
         /// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
         /// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
         /// <param name="page">用于显示PDF WebForm</param>
         /// <returns></returns>
         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;
              }
         }
         /// <summary>
         /// 返回Excel文件到用户的IE浏览器中
         /// </summary>
         /// <param name="ReportFile"></param>
         /// <param name="ReportDataSource"></param>
         /// <param name="page"></param>
         /// <returns></returns>
         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);
         }
         /// <summary>
         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
         /// 此方法的内容。
         /// </summary>
         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方法--------
         }       
     }
}

©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页
实付 99.00元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值