使用水晶报表输出PDF文件(类实例)

该博客介绍如何通过Crystal Reports库在.NET环境中创建PDF报告。首先引入必要的命名空间,然后定义一个Report类,该类根据传入的参数加载不同的报表(如Plastic或Part),并从SQL存储过程获取数据填充报表。最后,将报表导出为PDF文件并发送到HTTP响应,以便用户下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先:

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());
    }

   } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值