C# 如何不通过Crystal Reports Viewer直接打印报表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17377281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to directly print a Report without going through Crystal Reports Viewer
提问by Murali Bala
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?
所以帮助解决这个问题......如何直接打印而不通过Crystal Reports Viewer?
回答by Jayesh Goyani
Please use PrintToPrintermethod to directly print crystal report.
请使用PrintToPrinter方法直接打印水晶报表。
http://msdn.microsoft.com/en-us/library/aa691452%28VS.71%29.aspx
http://msdn.microsoft.com/en-us/library/aa691452%28VS.71%29.aspx
http://www.nullskull.com/q/10110521/print-crystal-report-without-viewing.aspx
http://www.nullskull.com/q/10110521/print-crystal-report-without-viewing.aspx
回答by Suresh E
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)
将这些代码与函数(参数)一起使用
回答by RoastBeast
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.
直接打印到打印机并不能解决您的问题。Crystal Report 要求正确设置参数,但出于某种原因并非如此。
回答by Er Ketan Vavadiya
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...
你应该试试这个……你肯定会成功……
回答by Er Ketan Vavadiya
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.PrintToPrintermethod prints the specified pages of the report to the printer selected with the help of the PrintOptions.PrinterNameproperty.
If no printer is selected, the default printer specified in the report will be used.
rptDoc.PrintToPrinter方法将报告的指定页面打印到在PrintOptions.PrinterName属性的帮助下选择的打印机。
如果未选择打印机,则将使用报告中指定的默认打印机。
We are using PrintToPrintermethod as :
我们使用的PrintToPrinter方法是:
public void PrintToPrinter (int nCopies , boolean collated , int startPage , int endPage );
where:
在哪里:
nCopiesindicates the number of copies to print.collatedindicates whether to collate the pages.startPageindicates the first page to print.endPageindicates the last page to print.
nCopies表示要打印的份数。collated指示是否整理页面。startPage表示要打印的第一页。endPage表示要打印的最后一页。
回答by Sagar Jadhav
So Simple
很简单
Remove thes lines:
crystalReportViewer1.ReportSource = objRpt; crystalReportViewer1.Refresh();Add this line:
objRpt.PrintToPrinter(1, false, 0, 0);
删除这些行:
crystalReportViewer1.ReportSource = objRpt; crystalReportViewer1.Refresh();添加这一行:
objRpt.PrintToPrinter(1, false, 0, 0);
回答by JIYAUL MUSTAPHA
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;
}
}

