如何不通过Crystal Reports Viewer直接打印报表

How to directly print a Report without going through Crystal Reports Viewer
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我写了这段代码以打印Crystal报表.但是出现错误

'缺少参数'...

ReportDocument rdoc = new ReportDocument();
rdoc .Load (Application.StartupPath +"\\"+@"REPORTS\SalaryReport.rpt");

rdoc.SetDataSource(ds.Tables[0]);

ParameterFields pfs = new ParameterFields();

ParameterField pfv = new ParameterField();
ParameterDiscreteValue pdv1 = new ParameterDiscreteValue();
pfv.Name = "fd";
pdv1.Value = fd;
pfv.CurrentValues.Add(pdv1);
pfs.Add(pfv);

ParameterField pfv1 = new ParameterField();
ParameterDiscreteValue pdv11 = new ParameterDiscreteValue();
pfv1.Name = "td";
pdv11.Value = td;
pfv1.CurrentValues.Add(pdv11);
pfs.Add(pfv1);

ParameterField pfv2 = new ParameterField();
ParameterDiscreteValue pdv12 = new ParameterDiscreteValue();
pfv2.Name = "department";
pdv12.Value = Dept;
pfv2.CurrentValues.Add(pdv12);
pfs.Add(pfv2);
crystalReportViewer1.ParameterFieldInfo = pfs;
crystalReportViewer1.ReportSource = rdoc;

PrinterSettings getprinterName = new PrinterSettings();
rdoc.PrintOptions.PrinterName = getprinterName.PrinterName;
rdoc.PrintToPrinter(1, true, 1, 1);

因此,有助于解决此问题....如何直接打印而不通过Crystal Reports Viewer?

解决过程1

很简单

  1. 删除这些行:

    crystalReportViewer1.ReportSource = objRpt;
    crystalReportViewer1.Refresh();
    
  2. 添加此行:

    objRpt.PrintToPrinter(1, false, 0, 0);
    
解决过程2

请使用PrintToPrinter方法直接打印水晶报表.

http://msdn.microsoft.com/zh-CN/library/aa691452%28VS.71%29.aspx

http://www.nullskull.com/q/10110521/print-crystal-report-without-viewing.aspx

速聊1:
我在报告中不使用ParameterFields的使用意味着我得到了输出,但是我在ParameterFields中使用了意味着我得到了"缺少参数"的错误,...
解决过程3
reportname report1=new reportname(); 
report1.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
report1.PrintOptions.PaperSize = PaperSize.PaperA4;
report1.PrintToPrinter(1, false, 0, 15);

将这些代码与功能一起使用(参数)

解决过程4

直接打印到打印机无法解决您的问题.Crystal Report要求正确设置参数,并且出于某些原因它们不是必需的.

解决过程5
  List<BusLib.Report.ReportParameter> ParaList = new List<BusLib.Report.ReportParameter>();
            ParaList.Add(new BusLib.Report.ReportParameter("Para1", Value1));
            ParaList.Add(new BusLib.Report.ReportParameter("Para2", Value2));
            ParaList.Add(new BusLib.Report.ReportParameter("Para3", Value3));
            ParaList.Add(new BusLib.Report.ReportParameter("Para4", Value4));

在那之后..

public void SetParameters(List<BusLib.Report.ReportParameter> pParams)
    {
        if (pParams == null) { return; }
        try
        {
            foreach (BusLib.Report.ReportParameter pPara in pParams)
            {
                CReport.SetParameterValue(pPara.ParameterName, pPara.ParameterValue);
            }
        }
        catch (Exception Ex)
        {
            Val.Message(Ex.Message.ToString());
        }
    }

您应该尝试一下...您将获得绝对的成功...

解决过程6
private void PrintReport(string reportPath, string PrinterName)
{
    CrystalDecisions.CrystalReports.Engine.ReportDocument rptDoc =
                        new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    rptDoc.Load(reportPath);

    CrystalDecisions.Shared.PageMargins objPageMargins;
    objPageMargins = rptDoc.PrintOptions.PageMargins;
    objPageMargins.bottomMargin = 100;
    objPageMargins.leftMargin = 100;
    objPageMargins.rightMargin = 100;
    objPageMargins.topMargin = 100;
    rptDoc.PrintOptions.ApplyPageMargins(objPageMargins);
    //rptDoc.PrintOptions.PrinterName = PrinterName;
    rptDoc.PrintToPrinter(1, false, 0, 0);
}

private void PrintToPrinter()
{
    PrintReport(System.Windows.Forms.Application.StartupPath +"\\VCrpfrmprint.rpt","Send To OneNote 2010");
}

rptDoc.PrintToPrinter方法将报表的指定页面打印到借助该PrintOptions.PrinterName属性选择的打印机上.
如果未选择打印机,则将使用报告中指定的默认打印机.

我们使用PrintToPrinter方法为:

public void PrintToPrinter (int nCopies , boolean collated , int startPage , int endPage );

在哪里:

  • nCopies 表示要打印的份数.
  • collated 指示是否整理页面.
  • startPage 指示要打印的第一页.
  • endPage 指示要打印的最后一页.
解决过程7
 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (MessageBox.Show("Do you want to Print/View P.O? Please be patient as P.O may take few seconds to load.", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                pl.POId = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                DataTable dt = new DataTable();
                dt = bl.PurchaseOrderPrint(pl);
                if (dt.Rows.Count > 0)
                {
                    Reports.PuchaseOrder rpt = new Reports.PuchaseOrder();
                    Print f = new Print();
                    rpt.SetDataSource(dt);
                    f.CRV.ReportSource = rpt;
                    f.Show();
                }
            }
            else
            {
                return;
            }
        }

I wrote this code to print a Crystal report.. but I got error

'missing parameters'...

ReportDocument rdoc = new ReportDocument();
rdoc .Load (Application.StartupPath +"\\"+@"REPORTS\SalaryReport.rpt");

rdoc.SetDataSource(ds.Tables[0]);

ParameterFields pfs = new ParameterFields();

ParameterField pfv = new ParameterField();
ParameterDiscreteValue pdv1 = new ParameterDiscreteValue();
pfv.Name = "fd";
pdv1.Value = fd;
pfv.CurrentValues.Add(pdv1);
pfs.Add(pfv);

ParameterField pfv1 = new ParameterField();
ParameterDiscreteValue pdv11 = new ParameterDiscreteValue();
pfv1.Name = "td";
pdv11.Value = td;
pfv1.CurrentValues.Add(pdv11);
pfs.Add(pfv1);

ParameterField pfv2 = new ParameterField();
ParameterDiscreteValue pdv12 = new ParameterDiscreteValue();
pfv2.Name = "department";
pdv12.Value = Dept;
pfv2.CurrentValues.Add(pdv12);
pfs.Add(pfv2);
crystalReportViewer1.ParameterFieldInfo = pfs;
crystalReportViewer1.ReportSource = rdoc;

PrinterSettings getprinterName = new PrinterSettings();
rdoc.PrintOptions.PrinterName = getprinterName.PrinterName;
rdoc.PrintToPrinter(1, true, 1, 1);

So help to solve this issue....how to print directly without going through Crystal Reports Viewer?

Solutions1

So Simple

  1. Remove thes lines:

    crystalReportViewer1.ReportSource = objRpt;
    crystalReportViewer1.Refresh();
    
  2. Add this line:

    objRpt.PrintToPrinter(1, false, 0, 0);
    
Solutions2

Please use PrintToPrinter method to directly print crystal report.

http://msdn.microsoft.com/en-us/library/aa691452%28VS.71%29.aspx

http://www.nullskull.com/q/10110521/print-crystal-report-without-viewing.aspx

Talk1:
Am use without ParameterFields in my report means i got output but i am use ParameterFields means i got error 'missing parameters',...
Solutions3
reportname report1=new reportname(); 
report1.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
report1.PrintOptions.PaperSize = PaperSize.PaperA4;
report1.PrintToPrinter(1, false, 0, 15);

use these codes with function(Parameters)

Solutions4

Printing directly to the printer is not going to solve your problem. The Crystal Report requires parameters to be set correctly and they're not for some reason.

Solutions5
  List<BusLib.Report.ReportParameter> ParaList = new List<BusLib.Report.ReportParameter>();
            ParaList.Add(new BusLib.Report.ReportParameter("Para1", Value1));
            ParaList.Add(new BusLib.Report.ReportParameter("Para2", Value2));
            ParaList.Add(new BusLib.Report.ReportParameter("Para3", Value3));
            ParaList.Add(new BusLib.Report.ReportParameter("Para4", Value4));

After That..

public void SetParameters(List<BusLib.Report.ReportParameter> pParams)
    {
        if (pParams == null) { return; }
        try
        {
            foreach (BusLib.Report.ReportParameter pPara in pParams)
            {
                CReport.SetParameterValue(pPara.ParameterName, pPara.ParameterValue);
            }
        }
        catch (Exception Ex)
        {
            Val.Message(Ex.Message.ToString());
        }
    }

you should try this ... you will get definetly success...

Solutions6
private void PrintReport(string reportPath, string PrinterName)
{
    CrystalDecisions.CrystalReports.Engine.ReportDocument rptDoc =
                        new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    rptDoc.Load(reportPath);

    CrystalDecisions.Shared.PageMargins objPageMargins;
    objPageMargins = rptDoc.PrintOptions.PageMargins;
    objPageMargins.bottomMargin = 100;
    objPageMargins.leftMargin = 100;
    objPageMargins.rightMargin = 100;
    objPageMargins.topMargin = 100;
    rptDoc.PrintOptions.ApplyPageMargins(objPageMargins);
    //rptDoc.PrintOptions.PrinterName = PrinterName;
    rptDoc.PrintToPrinter(1, false, 0, 0);
}

private void PrintToPrinter()
{
    PrintReport(System.Windows.Forms.Application.StartupPath +"\\VCrpfrmprint.rpt","Send To OneNote 2010");
}

rptDoc.PrintToPrinter method prints the specified pages of the report to the printer selected with the help of the PrintOptions.PrinterName property.
If no printer is selected, the default printer specified in the report will be used.

We are using PrintToPrinter method as :

public void PrintToPrinter (int nCopies , boolean collated , int startPage , int endPage );

where:

  • nCopies indicates the number of copies to print.
  • collated indicates whether to collate the pages.
  • startPage indicates the first page to print.
  • endPage indicates the last page to print.
Solutions7
 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (MessageBox.Show("Do you want to Print/View P.O? Please be patient as P.O may take few seconds to load.", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                pl.POId = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                DataTable dt = new DataTable();
                dt = bl.PurchaseOrderPrint(pl);
                if (dt.Rows.Count > 0)
                {
                    Reports.PuchaseOrder rpt = new Reports.PuchaseOrder();
                    Print f = new Print();
                    rpt.SetDataSource(dt);
                    f.CRV.ReportSource = rpt;
                    f.Show();
                }
            }
            else
            {
                return;
            }
        }
转载于:https://stackoverflow.com/questions/17377281/how-to-directly-print-a-report-without-going-through-crystal-reports-viewer

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入