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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://david.blog.csdn.net/article/details/4674219

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


 


 

1348118583_4652.jpg
展开阅读全文

水晶报表调用以及设计问题?

12-28

水晶报表是一个报表设计开发的强大工具,功能强大,设计灵活,在水晶报表光盘中只提供了一个完全动态生成报表的例子,使用繁琐。现介绍其在VC++6.0中的简单使用方法。编译环境:VC++6.0 sp5 、Windows 2000 Server sp3 (en)。 rnrn  一、导入水晶报表使用的动态联接库:根据实际修改文件路径。rnrn#import "C:\Program Files\Common Files\Crystal Decisions\2.5\bin\craxdrt.dll" no_namespace rnrn  二、定义接口指针变量rnrn//水晶报表变量rnrnIApplicationPtr m_Application;rnrnIReportPtr m_Report;rnrn//水晶报表控件变量,在对话框中加入该控件rnrnCCrystalReportViewer9 m_CRViewer1; rnrn  三、具体实现步骤rnrn//实例化m_Applicationrnrnm_Application.CreateInstance (__uuidof(Application));rnrn//获取m_Report变量rnrn//staff.rpt为通过向导建立的报表文件,数据库采用SQL Server 7.0rnrnm_Report =m_Application->OpenReport ("C:\\Documents and Settings\\Administrator\\桌面\\cr\\debug\\staff.rpt");rnrn//设置报表标题rnrnm_Report->put_ReportTitle (_bstr_t("Title"));rnrn//设置数据库连接变量rnrn//数据库服务器(local),数据库名staff,用户名sa,密码sarnrnm_Report->Database ->Tables ->Item [1]->SetLogOnInfo("(local)","staff","sa","sa");rnrn//设置检索SQL命令rnrnm_Report->put_SQLQueryString ((_bstr_t)"select * from person where id<"4" order by id");rnrn//不显示重复字段rnrnm_Report->PutEnableSelectDistinctRecords (TRUE);rnrn//设置检索条件,采用水晶报表语法,功能同设置检索SQL命令rnrnm_Report->PutRecordSelectionFormula ((_bstr_t)"person.id="1"");rnrn//设置报表作者rnrnm_Report->PutReportAuthor ("xiaojin");rnrn//将m_Report与报表控件连接rnrnm_CRViewer1.SetReportSource(m_Report);rnrn//显示报表工具条rnrnm_CRViewer1.SetDisplayToolbar (TRUE);rnrn//不显示报表左边的分组树rnrnm_CRViewer1.SetDisplayGroupTree (FALSE);rnrn//不显示控件边框rnrnm_CRViewer1.SetDisplayBorder (FALSE);rnrn//刷新数据rnrnm_CRViewer1.Refresh ();rnrn//显示报表内容rnrnm_CRViewer1.ViewReport(); 论坛

没有更多推荐了,返回首页