Crystal Reports直接下载到pdf C#

时间:2017-01-12 07:15:12

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

我想在CommonReportViewer.aspx页面中一起直接将水晶报告下载到pdf中,我在本地服务器上运行我的项目,源代码是MYPROJECT和(.rpt)报告 D:\ MYPROJECT \ Reporting \ Deposit \报告即可。我在Stackoverflow上尝试了不同的解决方案,即 ExportToDisk,ExportOptions 等等,但无法获得解决方案。在我的情况下,我查看银行报告,我通过表格提供报告参数,最后到达报告参数和报告显示。我的问题是如何直接以pdf格式下载该报告,因为我需要打印多页报告。

CommonReportViewer.aspx.cs

  public partial class CommonReportViewer : System.Web.UI.Page
    {
        ReportDocument mainDoc = new ReportDocument();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                CrystalReport report = new CrystalReport();
                report = (CrystalReport)Session["NLK_REPORT"];
                DateTime t1 = DateTime.Now;
                mainDoc.Load(report.ReportFilePath, OpenReportMethod.OpenReportByDefault);
                mainDoc.RecordSelectionFormula = report.SelectionCriteria;

                mainDoc.Refresh();
                string server = "NLKNEWDB";
                string userID = report.UserID;
                string password = report.Password;

                this.ApplyLogonInfo(mainDoc, server, userID, password);


                foreach (ReportParameter param in report.ParamList)
                {
                    mainDoc.SetParameterValue(param.ParamName, param.ParamValue);
                }

                foreach (SubReport sr in report.SubReportList)
                {
                    this.ApplyLogonInfo(mainDoc, server, userID, password);
                    foreach (ReportParameter param in sr.ParamList)
                    {

                        mainDoc.SetParameterValue(param.ParamName, param.ParamValue, sr.SubReportName);
                    }
                }

                mainDoc.SetDatabaseLogon(userID, password, server, "");

                this.crptViewer.ReportSource = mainDoc;

                mainDoc.DataSourceConnections.Clear();


            }
            finally
            {

            }
        }

        void ApplyLogonInfo(ReportDocument report, string server, string userID, string password)
        {
            TableLogOnInfo info = new TableLogOnInfo();
            ConnectionInfo cinfo = new ConnectionInfo();

            cinfo.ServerName = server;
            cinfo.UserID = userID;
            cinfo.Password = password;
            info.ConnectionInfo = cinfo;

            foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in report.Database.Tables)
            {
                tbl.ApplyLogOnInfo(info);
            }
        }

        private void Page_Unload(object sender, System.EventArgs e)
        {
            if (mainDoc != null)
            {
                mainDoc.Close();
                mainDoc.Dispose();
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

应该进行一些更改以将其导出为PDF格式。页面加载时,将Page_Load方法更改为。

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                CrystalReport report = new CrystalReport();
                report = (CrystalReport)Session["NLK_REPORT"];
                DateTime t1 = DateTime.Now;
                mainDoc.Load(report.ReportFilePath, OpenReportMethod.OpenReportByDefault);
                mainDoc.RecordSelectionFormula = report.SelectionCriteria;
                mainDoc.Refresh();

                string server = "NLKNEWDB";
                string userID = report.UserID;
                string password = report.Password;
                string empId = null;

                this.ApplyLogonInfo(mainDoc, server, userID, password);

                foreach (ReportParameter param in report.ParamList)
                {
                    mainDoc.SetParameterValue(param.ParamName, param.ParamValue);
                    if (param.ParamName == "P_EMPLOYER_ID")
                    {
                        empId = param.ParamValue.ToString();
                    }
                }

                foreach (SubReport sr in report.SubReportList)
                {
                    this.ApplyLogonInfo(mainDoc, server, userID, password);
                    foreach (ReportParameter param in sr.ParamList)
                    {

                        mainDoc.SetParameterValue(param.ParamName, param.ParamValue, sr.SubReportName);
                    }
                }

                mainDoc.SetDatabaseLogon(userID, password, server, "");
                mainDoc.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, empId);
                this.crptViewer.ReportSource = mainDoc;

                mainDoc.DataSourceConnections.Clear();




            }
            finally
            {

            }
        }
相关问题