使用水晶报表输出PDF文件(类实例)
首先:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
然后调用类即
public class Report
{
string Flag;
public Report(string flag)
{
Flag=flag;
}
public string ReturnFlag
{
get
{
return Flag;
}
}
public void GetReport()
{
try
{
string flag;
string FileName;
ReportDocument Cryreport = new ReportDocument();
TableLogOnInfo LogOnInfo =new CrystalDecisions.Shared.TableLogOnInfo(); //login on system information
DiskFileDestinationOptions Disoption =new CrystalDecisions.Shared.DiskFileDestinationOptions();
SqlConnection con=new SqlConnection();
SqlCommand cmd=new SqlCommand();
SqlDataAdapter adp=new SqlDataAdapter();
con.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
con.Open();
cmd.Connection = con;
flag=ReturnFlag;
Cryreport.Load(System.Web.HttpContext.Current.Server.MapPath(flag+".rpt"));
if (flag=="Plastic")
{
DataSet Plastic = new DataSet();
cmd.CommandText="Sp_Get_Plastic";
cmd.CommandType=CommandType.StoredProcedure;
adp.SelectCommand=cmd;
adp.Fill(Plastic,"Plastic");
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.ServerName = "127.0.0.1";
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.DatabaseName="BOM";
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.UserID="EF";
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.Password="EF";
//Cryreport.Database.Tables[0].LogOnInfo.TableName="Plastic";
//Cryreport.Database.Tables[0].SetDataSource(Plastic.Tables[0]);
Cryreport.SetDataSource(Plastic.Tables[0]);
}
else if (flag=="Part")
{
DataSet Part =new DataSet();
cmd.CommandText="Sp_Get_Part";
cmd.CommandType=CommandType.StoredProcedure;
adp.SelectCommand=cmd;
adp.Fill(Part,"Part");
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.ServerName = "127.0.0.1";
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.DatabaseName="BOM";
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.UserID="EF";
//Cryreport.Database.Tables[0].LogOnInfo.ConnectionInfo.Password="EF";
//Cryreport.Database.Tables[0].LogOnInfo.TableName="Part";
//Cryreport.Database.Tables[0].SetDataSource(Part.Tables[0]);
Cryreport.SetDataSource(Part.Tables[0]);
}
FileName = System.Web.HttpContext.Current.Server.MapPath("Report/") + System.Web.HttpContext.Current.Session.SessionID.ToString() + ".pdf";
Cryreport.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
Cryreport.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
Disoption.DiskFileName = FileName;
Cryreport.ExportOptions.DestinationOptions = Disoption;
Cryreport.Export();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
System.Web.HttpContext.Current.Response.WriteFile(FileName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Close();
System.IO.File.Delete(FileName);
}
catch (System.Exception ex)
{
System.Web.HttpContext.Current.Response.Write("错误提示:"+ex.ToString());
}
}
posted on 2006-12-20 10:37 hnsjack(痞子龍) 阅读(108) 评论(0) 编辑 收藏 举报