如何在传递多个参数的同时生成PDF格式的Crystal Report?

时间:2011-02-24 12:04:00

标签: c# asp.net crystal-reports pdf-generation

我想以pdf格式生成水晶报告。我通过传递一个参数做了同样的事情。但这次我要传递10个参数。我遵循了与传递一个参数所做的相同的事情。

但现在我收到错误消息“无法评估表达式,因为代码已经过优化,或者本机框架位于调用堆栈之上。” 有人请提出建议。提前致谢。 请根据生成PDF格式的Crystal Report修改我的代码。

在按钮点击事件中,我编写了以下代码。

try
    {
        CrystalDecisions.CrystalReports.Engine.ReportDocument rpt =
                        new CrystalDecisions.CrystalReports.Engine.ReportDocument();

        string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        string[] str = conn.Split(';');
        string server = str[0].Substring(str[0].IndexOf(" = ") + 3);
        string database = str[1].Substring(str[1].IndexOf(" = ") + 3);
        string userid = str[2].Substring(str[2].IndexOf(" = ") + 3);
        string password = "Welc0me";

        rpt.Load(Server.MapPath("~/Reports/Marketing/JobOrdersList.rpt"));

        for (int i = 0; i < rpt.DataSourceConnections.Count; i++)
            rpt.DataSourceConnections[i].SetConnection(server, database, userid, password);
        rpt.SetParameterValue(0, DateTime.ParseExact(dcfromdate.DateString.ToString(), DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None));
        rpt.SetParameterValue(1, DateTime.ParseExact(dcTodate.DateString.ToString(), DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None));
        rpt.SetParameterValue(2, ddlCompany.SelectedValue);
        rpt.SetParameterValue(3, ddlUnit.SelectedValue);
        rpt.SetParameterValue(4, ddlCustomer.SelectedValue);
        rpt.SetParameterValue(5, ddlProduct.SelectedValue);
        rpt.SetParameterValue(6, ddlScope.SelectedValue);
        rpt.SetParameterValue(7, ddlStatus.SelectedValue);
        rpt.SetParameterValue(8, ddlGroupBy.SelectedValue);
        rpt.SetParameterValue(9, (ChkPrint.Checked == true ? "True" : "False"));
        rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, "JobOrderList Report");

    }
    catch (Exception ex)
    {
        return ex.Message.ToString();
    }

1 个答案:

答案 0 :(得分:3)

//To generate PDF dynamically 

using System;
    using System.Windows.Forms;
    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            ReportDocument cryRpt;

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                cryRpt = new ReportDocument();
                cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt");
                crystalReportViewer1.ReportSource = cryRpt;
                crystalReportViewer1.Refresh(); 
            }

            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    ExportOptions CrExportOptions ;
                    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                    PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                    CrDiskFileDestinationOptions.DiskFileName = "c:\\csharp.net-informations.pdf";
                    CrExportOptions = cryRpt.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    cryRpt.Export();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }