水晶报表导出PDF or Excel

Filename 可以是文件名,也可以是路径+ 文件名
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
{
    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)
        {
            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();
            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");
            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);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值