【问题标题】:How to Automatically Print Crystal Reports with Default Printer如何使用默认打印机自动打印 Crystal Reports
【发布时间】:2014-02-25 06:15:12
【问题描述】:

我在使用水晶报表自动打印报表时遇到问题。下面是我的代码sn-p

它正在打印,但问题是它忽略了 ReportView.SelectionFormula(仅包含第 1 页到第 10 页)但打印数据源中的所有记录。

 ConnectionInfo ConInfo;
        try
        {
            this.Cursor = Cursors.WaitCursor;


            ConInfo = ConfigureCrystalReportsRD();

            ReportDocument.Load(reportToLoad);
            ReportView.ReportSource = ReportDocument;
            SetDBLogonForReportRD(ConInfo, ReportDocument);
            SetReserveFormulaValue();


            string strReportFilter = "";

            strReportFilter = ReportDocument.DataDefinition.RecordSelectionFormula;

            if (strReportFilter != "" && formulaFields != "")
            {
                ReportView.SelectionFormula = strReportFilter + " and " + formulaFields;
            }
            else
            {
                ReportView.SelectionFormula = formulaFields;
            }


            if (isPint == true)
            {
                this.Cursor = Cursors.WaitCursor;
                System.Drawing.Printing.PrinterSettings printer = new System.Drawing.Printing.PrinterSettings();
                System.Drawing.Printing.PageSettings page = new System.Drawing.Printing.PageSettings();
                ReportDocument.PrintToPrinter(printer,page,true);
                MessageBox.Show("Printing at " + printer.PrinterName + " .....");
                this.Cursor = Cursors.Default;

            }

            this.Cursor = Cursors.Default;


        }
        catch (Exception e)
        {
            oGenMethod.ErrorMessage(e.Message, FORMID, "PreviewReport");
        }

注意:

ReportView.PrintReport() 成功完成这项工作,但它会弹出打印机设置

提前谢谢你!

编辑:

我的代码中的错误是我只在 ReportViewer 对象上设置了 RecordSelectionFormula,而不是在报告文档上。

ConnectionInfo ConInfo;

        string strReportFilter = "";

        try
        {
            this.Cursor = Cursors.WaitCursor;


            ConInfo = ConfigureCrystalReportsRD();

            ReportDocument.Load(reportToLoad);
            ReportView.ReportSource = ReportDocument;
            SetDBLogonForReportRD(ConInfo, ReportDocument);
            SetReserveFormulaValue();


            strReportFilter = ReportDocument.DataDefinition.RecordSelectionFormula;

            if (strReportFilter != "" && formulaFields != "")
                ReportView.SelectionFormula = strReportFilter + " and " + formulaFields;
            else
                ReportView.SelectionFormula = formulaFields;


            ReportDocument.DataDefinition.RecordSelectionFormula = ReportView.SelectionFormula;




            if (isPint == true)
            {
                this.Cursor = Cursors.WaitCursor;
                ReportDocument.PrintToPrinter(1, true, 0, 0);
                this.Cursor = Cursors.Default;              

}

【问题讨论】:

    标签: c# winforms crystal-reports ado.net


    【解决方案1】:

    您好,如果您不想显示弹出窗口,您必须使用 ReportDocument 的 PrintToPrinter 方法。

    所以你应该在你的代码中做这样的事情

    ReportDocument.Load(reportToLoad);
    SetDBLogonForReportRD(ConInfo, ReportDocument);
    SetReserveFormulaValue();
    strReportFilter = ReportDocument.DataDefinition.RecordSelectionFormula;
    if (strReportFilter != "" formulaFields != "")
    ReportDocument.DataDefinition.RecordSelectionFormula += " and " + formulaFields;
    else
    ReportDocument.DataDefinition.RecordSelectionFormula = formulaFields;
    ReportDocument.PrintToPrinter(1, true, 0, 0);
    

    这应该可以解决您的问题。

    正如您在此处询问的那样,我用于测试选择公式和 PrintToPrinter 方法的代码。首先,我根据我的帐户表创建了一个非常简单的报告,我通过代码过滤了我的代码,以便我得到一个帐户。

    var cr = new ReportDocument();
    cr.Load(@"c:\Reports\Report1.rpt");
    cr.DataDefinition.RecordSelectionFormula = "{Account.Code} = '10000'";
    cr.PrintToPrinter(1, true, 0, 0);
    

    此代码打印我的报告,上面有一条记录,如果我评论 RecordSelectionFormulan 行,报告会打印整个帐户列表

    【讨论】:

    • 我尝试了该代码,但问题是 ReportView.SelectionFormula 中的公式没有被应用。
    • 我更新了示例,我不得不去掉 if 中的 AND 子句,因为当我试图保存帖子时它给了我一个错误。这样你就不需要报表查看器对象,您可以过滤您的报告并直接在默认打印机上打印
    • ReportDocument.PrintToPrinter(1, true, 0, 0); - 是的,它的打印,但打印数据源中的所有页面。它忽略了 RecordSelectionFormula
    • 这听起来很奇怪,我刚刚在一个测试项目中尝试过,数据在打印之前被过滤掉了。我应该看看你修改过的代码,看看问题是否存在。
    • 如果您只需要打印报告,您可以从页面中删除 ReportView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多