如何使用默认打印机自动打印Crystal Reports

时间:2014-02-25 06:15:12

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

我在使用水晶报表自动打印报表时遇到问题。以下是我的代码段

它是打印但问题是它忽略 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,而不是在Report Document上。

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;              

}

1 个答案:

答案 0 :(得分:3)

如果您不想显示弹出窗口,则必须使用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行,报告将打印整个帐户列表