[水晶报表]水晶报表创建以及调用方法

本文详细介绍了在ASP.NET环境中创建及使用水晶报表的方法,包括通过拉模式和推模式设置数据源,以及使用存储过程填充数据集的具体步骤。

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

ASP.NET2.0/3.5/4.0水晶报表创建以及调用方法
1. 创建CrystalReport(网站中添加新项CrystalReport,或者单独开发水晶报表)放到指定目录
2、使用向导创建报表(如何创建请参考其它资料)Suppliers.rpt
3、VS.NET2005/2008/2010布局报表,添加删除字段.
4、回到页面,选择控件CrystalReportViewer将其放到页面上。
5、设置CrystalReportViewer1数据源(介绍两种方法)。

引入

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;

第一种: 拉模式

    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
            AutoDataBind="True" GroupTreeImagesFolderUrl="" Height="1202px" 
         ReportSourceID="CrystalReportSource1" ToolbarImagesFolderUrl="" 
         ToolPanelWidth="200px" Width="1104px"   ToolPanelView="None" />
     <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
         <Report FileName="..\..\ERP\Reports\ZMQuotation.rpt">
         </Report>
     </CR:CrystalReportSource>

CS:

      CrystalReportSource1.ReportDocument.SetDatabaseLogon(PublicVar.ShatdbID, PublicVar.shatdbPW, PublicVar.ShatdbDS, PublicVar.Shatdb);//取得公共变量
      CrystalReportSource1.ReportDocument.SetParameterValue("@ItemP", "no");
      CrystalReportSource1.ReportDocument.SetParameterValue("@descriptionP", "no");
      CrystalReportViewer1.DataBind();

     注释:PublicVar.ShatdbID 用户名, PublicVar.shatdbPW 密码, PublicVar.ShatdbDS 数据源(机器名或者IP), PublicVar.Shatdb 数据库名
          CrystalReportSource1.ReportDocument.SetParameterValue("@ItemP", "no"); 报表参数
     这种方法通常第一次登陆的时候就需要给定一个默认参数(如果带参数)
第二种:推模式

        string DBConfig_sql = PublicVar.strCon;
        DataSet ds = new DataSet();
        SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
        SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
        SqlDataAdapter sqlAd = new SqlDataAdapter();
        sqlAd.SelectCommand = sqlCmd;
        sqlAd.Fill(ds, "sql");
        //如果绑定报表
        //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
        CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]);
        CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind();
        //未绑定
        String path = Server.MapPath("HandQty.rpt");
        CrystalReportViewer1.Load(path);
        CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]);
        CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind();

存储过程:

        ReportDocument myReport = new ReportDocument();
        SqlParameter[] parms = new SqlParameter[1];
        parms[0] = new SqlParameter("@id", SqlDbType.Int);
        parms[0].Value = id; 
        DataSet ds = new DataSet();
        ds = DataAccess.DataAccessSHDB.GetTableByStore("project", parms).DataSet;
 
        CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
        string reportPath = Server.MapPath("..//Reports//project.rpt");
        //doc.Load(@"C:/test/1.rpt");
        doc.Load(reportPath);
        doc.SetDataSource(ds.Tables[0]);
        CrystalReportViewer1.ReportSource = doc;
        CrystalReportViewer1.DataBind();

如果有时候无法刷新数据:添加如下代码即可

      CrystalReportSource1.ReportDocument.SetParameterValue("@ItemP", "no");
      CrystalReportSource1.ReportDocument.SetParameterValue("@descriptionP", "no");
      CrystalReportViewer1.DataBind();


第三种方式:

直接show在页面,不含水晶报表工具栏;

private void Print()
    {
        SampleRequest sr = new SampleRequest();
        sr.TaskCode = taskcode;
        DataSet ds = new DataSet();
        ds = sr.SampleRequestDetail();
        if (ds != null)
        {
            CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            string reportPath = Server.MapPath("..\\..\\ERP\\Reports\\SampleRequest.rpt");
            doc.Load(reportPath);
            doc.SetDataSource(ds.Tables[0]);

            CrystalReportViewer1.ReportSource = doc;
            CrystalReportViewer1.DataBind();

            string filetype = "";
            filetype = "PDF";
            string contenttype = "";
            //string myfilename = Request.MapPath(".\\") + Session.SessionID + "." + filetype;//目录地址 注意反义字符
            string myfilename = "E:\\Report\\SAL\\" + "SampleRequest" + Baseclass.getLongData().ToString() + "." + filetype;
            CrystalDecisions.Shared.DiskFileDestinationOptions mydiskoptions = new CrystalDecisions.Shared.DiskFileDestinationOptions();
            mydiskoptions.DiskFileName = myfilename;
            CrystalDecisions.Shared.ExportOptions myExportOptions = new ExportOptions();//oCR.ExportOptions; 
            myExportOptions.DestinationOptions = mydiskoptions;
            myExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;

            contenttype = "application/pdf";
            myExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;

            doc.Export(myExportOptions); //输出报表 到服务器端的制定路径 
            Response.ClearContent();     //清空页面 
            Response.ClearHeaders();
            Response.ContentType = contenttype;
            Response.WriteFile(myfilename);//直接写入页面,页面平铺 可实现在线阅读功能
            Response.Flush();
            Response.Close();
        }
        else
        {
            JScript.Alert("没有找到记录", this.Page);
        }
    }


 


 

Crystal Reports for Visual Studio 2005 SDK Tutorials: Sample Code Projects 1. This setup contains sample code projects for Crystal Reports for Visual Studio 2005. 2. This sample code corresponds exactly to the tutorials contained in the Crystal Reports for Visual Studio 2005 online help. Each project has been built by following the instructions in their corresponding tutorials, and instructions and explanations have been placed within the tutorial sections rather than in code comments. Therefore, it is highly recommended that you consult the tutorials for a full explanation of the structure and best practices demonstrated within this code. 3. If you wish to run the tutorial sample code projects, check the code for any machine-specific values and confirm that they are customized for your specific machine. These may include: * ODBC data source settings for reports * file directory paths * network printer paths * API property settings for database connections, including: * database server names * database names * userIDs * passwords * complete database connection strings 4. For security reasons it is recommended that you use Integrated Security (Windows Authentication) for your SQL Server database connectivity. If you plan to use SQL Authentication, it is strongly recommended that you create a database account with limited access to your database. (Both versions of authentication are demonstrated in the tutorials.) For more information on security, including how to create a limited access database account, see the SDK Fundamentals section of the Crystal Reports for Visual Studio 2005 documentation. Asp.net2.0水晶报表(CrystalReports)事例源码大全(C#) 这些例子实现了水晶报表的查看、柱状图显示、打印、导出、缩放等基本功能,如果想了解更多比如说利用DataSet方式、Push、Pull等模式需要自己更深一步的研究了 难得的Asp.net2.0水晶报表源码,请珍藏!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值