zoukankan      html  css  js  c++  java
  • 水晶报表使用指南 转

    制作B/S的朋友肯定对Crystal Report绝不陌生。功能强大,方便。我在网上查找了许多关于Crystal Report的资料。对其做了整理、调试。在自己学习的同时,也希望拿出来与大家分享。下面是我关于Crystal Report的整理内容集锦。

      

    一、          Crystal报表的建立

    水晶报表在应用时分两种方法,分别是拉模式(PULL)、推模式(PUSH)。
    拉模式:在水晶报表生成时的数据源是从水晶报表文件中的SQL语句从数据库中提取的,在编程时不用重写SQL语句,但要加上登录信息。
    推模式:在水晶报表生成时的数据源,是用编程时重写水晶报表中SQL语句而生成的dataset对像。也就是说,推模式是用dataset组装水晶报表。

    水晶报表组件介绍:水晶报表在VS2005中有两种组件:
    WEB项 目中分别是CrystalReportSource,CrystalReportViewer。
    FORM项目里是分别是CrystalReport,CrystalReportViewer。
    CrystalReportSource,crystalReport是水晶报表的数据提供者;CrystalReportViewer是水晶报表的浏览 器。另外还要介绍一下水的报表的文件是以rpt为扩展名的文件,该文件可以用VS2005生成。
    WINFORM中的建立:
    1. 首先建立一个新的项目工程,在项目中添加一个Crystal报表。在报表的创建向导中我们创建一个新的OLE DB(ADO)连接,以便于我们对SQL数据库中表的连接。
    2. 在弹出的连接类型窗口内,选择Microsoft OLE DB Provider for SQL Server一项,以确立报表的数据连接为SQL服务类。
    3. 在连接信息当中选择所需使用的服务器名,并输入相应用户ID和密码,选择好报表所要连接的数据库后,新的数据库连接便建立完成。此时,我们可以从报表数据 连接窗口中看到我们所创建的报表,并选择需要的表,进行数据连接。
    4. 此后,我们可以根据报表向导进行表间关联、排序索引设置、;样式选择等表的初始化设置。完成Crystal报表与SQL服务器内数据连接以后,我们可以在 报表中预览所创建的报表样。
    5. 对Crystal报表进行保存后,在WINFORM的窗口中建立CrystalReportViewer控 件,生成水晶报表的预览窗口。在建立中,我们可以根据需要对该控件的添加项进行筛选,包括显示工具栏、显示组树、显示状态栏三项。
    6. 在WINFORM窗口中添加ReportDocument控件,并为该控件选择一个需要进行类型化的Report表单。
    WEBFORM中的建立:
    建立方法与WINFORM方式基本相同,但不需要建立ReportDocument。在建立过程中一定注意,在.aspx设 计器中建立CrystalReportViewer与CrystalReportsource两个控件,前者用作水晶报表的预览,后者起到 报表与预览连接的作用,负责建立CrystalReportViewer与Crystal报表间的连接。

    二、          报表数据的连接

    拉模式(PULL):
    WINFORM中建立连接:
    方式一:如果该页面只调用固定的一个Crystal报表,可以直接在CrystalReportViewer中进行选择报表,将Crystal报表与其进 行绑定。
    方式二:如果页面需根据具体要求,调用不同的报表进行显示,那么我们就需要通过对报表的连接的设置来完成这一功能。
    程序如下:

    //CrystalReport1.rpt是水晶报表文件的名称;CrystalReport11是从工具箱加到页面上的CrystalDocument
    crystalReport11.Load(Application.StartupPath + "CrystalReport1.rpt");
    //运用CrystalDocument读取所需Crystal表单路径;
          crystalReport11.SetDatabaseLogon("sa", "", @"BENQ-JAY", "Userdatabase");
    //进行CrystalDocument连接服务器的设置,其中第一参数:用户名;第二参数:密码;第三参数:服务器名;第四参数:数据库名
    crystalReportViewer1.ReportSource = crystalReport11;
    //将CrystalReportViewer的报表源指向CrystalDocument
    WEBFORM中建立连接:
    方式一:在CrystalReportSource中直接对表单源进行配置,选择需要宣示的表单。在CrystalReportViewer中将报表源指 定为CrystalReportSource,即可在该网页中生成指定报表的水晶报表。
    方式二:通过程序进行具体配置,将所需Crystal报表与预览页面进行连接。实现报表的灵活性选择。
    程序如下:

    // CrystalReport.rpt是水晶报表文件的名称;CrystalReportSource1是从工具箱加到页面上的水晶报表数据源对像。
    CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
    //读取Crystal表单所在路径,将其与CrystalReportSource进行绑定
             CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "", @"BENQ-JAY", "UserDatabase");
    //进行数据库连接,参数一:用户名;参数二:密码;参数三:服务器名;参数四:数据库名
             CrystalReportSource1.DataBind();
    //对CrystalReportSource进行数据绑定
             CrystalReportViewer1.ReportSource = CrystalReportSource1;
    //将预览源指定为CrystalReportSource
           CrystalReportViewer1.DataBind();
    //对CrystalReportViewer进行数据绑定

    推模式(PUSH):
    在推模式下,必须建立DataSet数据集,用来进行数据源填充。在推模式中编程组装的DataSet里的SQL语句中的字段要与水晶报表里的SQL语句 字段一致。

    WINFORM中建立连接:
    程序如下:
    string sql = "select * from UserInfor";
    string conn = "server=BENQ-JAY;Database=UserDatabase;uid=Sa;pwd=;";
             DataSet ds = new DataSet();
             SqlConnection con1 = new SqlConnection(conn);
             SqlCommand cmd1 = new SqlCommand(sql, con1);
             SqlDataAdapter sqlad = new SqlDataAdapter();
             sqlad.SelectCommand = cmd1;
             //获得一个TRANSACT-SQL存储过程,用于在数据源中选择记录
             sqlad.Fill(ds, "sql");
             //填充SQL命令所指定的行
             crystalReport11.Load(Application.StartupPath + "CrystalReport1.rpt");
             crystalReport11.SetDataSource(ds.Tables["sql"]);
             crystalReportViewer1.ReportSource = crystalReport11;
    WEBFORM中建立连接:
    程序如下:
             string sql = "select * from UserInfor";
             string con1 = "server=BENQ-JAY;Database=UserDatabase;uid=sa;pwd=;";
             DataSet ds = new DataSet();
             SqlConnection con = new SqlConnection(con1);
             SqlCommand com = new SqlCommand(sql, con);
             SqlDataAdapter sqlad = new SqlDataAdapter();
             sqlad.SelectCommand = com;
             sqlad.Fill(ds, "sql");
             CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
             CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]);
             CrystalReportSource1.DataBind();
             CrystalReportViewer1.ReportSource = CrystalReportSource1;
             CrystalReportViewer1.DataBind();

    三、          报表打印

    水晶报表的CrystalReportViewer中默认配备的工具栏可实现打印功能,但其功能比较单一,无法进行打印机选择。我们可以用代码设置的方法 进行Crystal表单的打印功能。

    WINFORM中的打印:
    代码如下:
    private void print_Click(object sender, EventArgs e)
           {
             try
             {
                CrystalDecisions.CrystalReports.Engine.ReportDocument rd = new CrystalDecisions.CrystalReports.Engine.ReportDocument();//加载表格的打印属性
                rd.Load("E:\\Projects\\test1\\test1\\CrystalReport1.rpt");//设定表格路径
                rd.PrintOptions.PrinterName = printerList.Text.ToString();//添加打印机
                rd.PrintToPrinter(1, true, 0, 0);//设置打印参数
             }
             catch (Exception ex)
             {
                MessageBox.Show(ex.Message.ToString());
             }
           }
    private void Form1_Load(object sender, EventArgs e)
           {
             try
             {
                PrintDocument prtdoc = new PrintDocument();
                string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//获取默认的打印机名
                foreach (String strPrinter in PrinterSettings.InstalledPrinters)
                //在列表框中列出所有的打印机,
                {
                       printerList.Items.Add(strPrinter);
                       if (strPrinter == strDefaultPrinter)//把默认打印机设为缺省值
                       {
                         printerList.SelectedIndex = printerList.Items.IndexOf(strPrinter);
                       }
                }
                crystalReport11.Load(Application.StartupPath + "CrystalReport1.rpt");//设置报表路径
                crystalReport11.SetDatabaseLogon("sa", "", @"BENQ-JAY", "Userdatabase");//设置服务器连接信息
                crystalReportViewer1.ReportSource = crystalReport11;//绑定报表
             }
             catch (Exception ex)
             {
                MessageBox.Show(ex.Message.ToString());
             }
    代码中,我们实现了水晶报表的选择,在进行水晶报表的添加后,我们可以从水晶报表窗口中看到我们添加的页面信息,从而实现打印预览。此外,我们在一个 combobox中实现了对本地打印机的枚举,在进行页面打印之前,可以先对打印机进行选择。从而使打印更加灵活。
    WEBFORM中的打印:
    在WEBFORM中实现页面加载与打印选择的方式与WINFORM中基本相同。不同之处在于:文件路径的选择方法上采用server.mappath() 方法。在枚举打印机时,我们使用DropDownList进行显示。
    代码如下:
    protected void Page_Load(object sender, EventArgs e)
    {
           try
           {
             CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
             CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "", @"BENQ-JAY", "UserDatabase");
             CrystalReportSource1.DataBind();
             CrystalReportViewer1.ReportSource = CrystalReportSource1;
             CrystalReportViewer1.DataBind();
             PrintDocument prtdoc = new PrintDocument();
             string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;//获取默认的打印机名
             foreach (String strPrinter in PrinterSettings.InstalledPrinters)
             //在列表框中列出所有的打印机,
             {
                DropDownList1.Items.Add(strPrinter);//使用DropDownList进行打印机枚举
             }
           }
           catch (Exception ex)
           {
             this.Response.Write("<script>alert('" + ex.ToString() + "')</script>");
           }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
           CrystalDecisions.CrystalReports.Engine.ReportDocument rd = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
           rd.Load(Server.MapPath("CrystalReport.rpt"));//从WEB服务器上获取文件物理位置
           rd.PrintOptions.PrinterName = DropDownList1.Text.ToString();
           rd.PrintToPrinter(1, true, 0, 0);
    }

    四、          数据导出
    水晶报表在C/S中的CrystalReportViewer控件可以实现数据的导出功能。包括PDF、XLS、DOC以及RTF格式。在B/S中则无法 实现数据导出的多样化。为此,我们通过对参数的设置,完成对水晶报表的数据导出。
    程序如下:
    private void ExportCrv(CrystalReport cr, string strType, string strPath)
    {
       DiskFileDestionOptions dOpt=new DiskFileDestionOptions();
    //设置并检索磁盘,确认导出文件类型
       cr.ExportOptions.ExportDestinationType=ExportDestinationType.DiskFile();
       //设置输出目标文件所指向的磁盘文件
       switch(strType)
       {
          case "RTF":
              cr.ExportOptions.ExportFormatType=ExportFormatType.RichText;
              dOpt.DiskFileName=strPath;
              break;
       //设定导出RTF文件,并确定导出路径
          case "PDF":
              cr.ExportOptions.ExportFormatType=ExportFormatType.PortableDocFormat;
              dOpt.DiskFileName=strPath;
              break;
       //设定导出PDF文件,并确定导出路径
          case "DOC":
              cr.ExportOptions.ExportFormatType=ExportFormatType.WordForWindows;
              dOpt.DiskFileName=strPath;
              break;
    //设定导出DOC文件,并确定导出路径
          case "XLS":
              cr.ExportOptions.ExportFormatType=ExportFormatType.Excel;
              dOpt.DiskFileName=strPath;
              break;
    //设定导出EXCEL文件,并确定导出路径
          default;
          break;
             
       }
       cr.ExportOptions.DestinationOptions=dOpt;
       cr.Export();
        //数据导出
    }

    ________________________________________________

    传参   
       string sql = "select * from contract where co_client='777'";
           DataSet ds = new DataSet();
           SqlConnection con = new SqlConnection(@"server=61.174.68.211;database=sales;uid=service;pwd=");
           SqlCommand com = new SqlCommand(sql, con);
           SqlDataAdapter sqlad = new SqlDataAdapter();
           sqlad.SelectCommand = com;
           sqlad.Fill(ds, "contract");
           CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
           CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["contract"]);

          TextObject obj =CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"] as TextObject;//根据文本传值到水晶报表
           obj.Text = "555";


           CrystalReportSource1.DataBind();
           CrystalReportViewer1.ReportSource = CrystalReportSource1;
           CrystalReportViewer1.DataBind();
  • 相关阅读:
    初学node.js-nodejs中实现用户登录路由
    初学node.js-nodejs中实现用户注册路由
    初学node.js-nodejs连接MongoDB(5)
    初学node.js-MongoDB安装和环境服务配置(4)
    初学node.js-nodejs中实现HTTP服务(3)
    初学node.js-npm使用(2)
    初学node.js-nodejs安装运行(1)
    python-两个图片相似度算法
    python-两个筛子数据可视化(直方图)
    从头学习网络编程——网络编程学习笔记 :什么是HTTP与CGI?
  • 原文地址:https://www.cnblogs.com/liye/p/1782413.html
Copyright © 2011-2022 走看看