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

时间:2013-06-29 06:12:49

标签: c# crystal-reports

我写了这段代码来打印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?

7 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

reportname report1=new reportname(); 
report1.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
report1.PrintOptions.PaperSize = PaperSize.PaperA4;
report1.PrintToPrinter(1, false, 0, 15);

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

  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());
        }
    }

你应该试试这个...你将获得绝对的成功......

答案 4 :(得分:0)

int main(){
int i, j, k, n;
float MatA[100][100], MatB[100], X[100];
float Divisor, Factor, sum;
cin >> n;

for(i=0; i< n; i++) {
    for(j=0; j < n; j++) {
        cin >> MatA[i][j];
    }
}

for(i=0; i< n; i++) {
    cin >> MatB[i];
}

for (i=0; i< n; i++) {
    Divisor = MatA[i][i];
    MatA[i][i] = 1.0;

    for (j = i+1; j < n; j++) {
        MatA[i][j] = MatA[i][j]/Divisor;
    }

    MatB[i] = MatB[i]/Divisor;

    if (i+1 < n) {
        for (k=i+1; k<n; k++) {
            Factor = MatA[k][i];
            MatA[k][i] = 0.0;
            for (j = i+1; j < n; j++) {
                MatA[k][j] = MatA[k][j] - Factor * MatA[i][j];
            }
            MatB[k] = MatB[k] - Factor * MatB[i];
        }
    }
}

X[n-1] = MatB[n-1];
for (i = n-2; i>=0; i--) {

    sum = 0.0;
    for (j = i+1; j < n; j++) {
        sum = sum + MatA[i][j] * X[j];
    }
    X[i] = MatB[i] - sum;
}

for(i=0; i< n; i++) {
    for (j = 0; j < n; j++) {
        cout << MatA[i][j] << " ";
    }
    cout << " " << MatB[i] << endl;
}
for (i=0; i<n; i++) {
    cout << "X[" << i << "] is: " ;
    cout << X[i] << endl;
}
return 0;
}

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表示要打印的最后一页。

答案 5 :(得分:0)

如此简单

  1. 删除行:

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

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

答案 6 :(得分:0)

 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;
            }
        }