您的位置:首页 > 其它

水晶报表导出PDF or Excel

2010-05-12 14:08 537 查看
在页面中加入CrystalReportViewer和Button:

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: