您的位置:首页 > 编程语言 > C#

c#中读水晶报表转为pdf的方法

2013-09-13 10:03 531 查看
 private voidCreateRpt(string pMapPath, string pRptID, string pFlag, string pHospNo, string pUsrID)

    {

        ReportDocument ReportDoc = new ReportDocument();

        string strData = System.DateTime.Now.ToString("yyyy/MM/dd").Replace("/", "-");

        string strServerPath = Server.MapPath("~");

        if (!strServerPath.EndsWith("\\"))

            strServerPath += "\\";

        string strReportName = strServerPath + "DISRPT\\" + "水晶报表的名字";// "Check.rpt";水晶报表的位置,即绝对路径

        ReportDoc.Load(strReportName);

        ReportDoc.SetParameterValue("UsrID", pUsrID);//向水晶报表传递的参数

        ReportDoc.SetParameterValue("HospNo", pHospNo);

        SetReportDBInf(ReportDoc.Database.Tables);

        string strPdfPath = "";//生成的pdf的存放路径

        if (!System.IO.Directory.Exists(strPdfPath))

        {

            System.IO.Directory.CreateDirectory(strPdfPath);

        }

        string PdfName = pRptID.ToUpper().Replace(".RPT", "") + ".pdf";

        strPdfPath += "\\" + PdfName;

        if (File.Exists(strPdfPath))

            File.Delete(strPdfPath);

        ReportDoc.ExportToDisk(ExportFormatType.PortableDocFormat, strPdfPath);

     }

//设定水晶报表连接数据库

 public void SetReportDBInf(CrystalDecisions.CrystalReports.Engine.Tables rptTables)

    {

        string strDBLink = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

        string[] aryDB = strDBLink.Split(';');

        try

        {

            string strServerName = "";

            string strDatabaseName = "";

            string strUserID = "";

            string strPassword = "";

            for (int i = 0; i < aryDB.Length; i++)

            {

                if (aryDB[i].Split('=')[0].ToString().ToUpper() == "SERVER")

                    strServerName = aryDB[i].Split('=')[1].ToString().Trim();

                if (aryDB[i].Split('=')[0].ToString().ToUpper() == "DATABASE")

                    strDatabaseName = aryDB[i].Split('=')[1].ToString().Trim();

                if (aryDB[i].Split('=')[0].ToString().ToUpper() == "USER ID")

                    strUserID = aryDB[i].Split('=')[1].ToString().Trim();

                if (aryDB[i].Split('=')[0].ToString().ToUpper() == "PASSWORD")

                    strPassword = aryDB[i].Split('=')[1].ToString().Trim();

            }

            CrystalDecisions.Shared.TableLogOnInfo tLogOnInfo;

            foreach (CrystalDecisions.CrystalReports.Engine.Table cTable in rptTables)

            {

                tLogOnInfo = cTable.LogOnInfo;

                tLogOnInfo.ConnectionInfo.ServerName = strServerName;

                tLogOnInfo.ConnectionInfo.DatabaseName = strDatabaseName;

                tLogOnInfo.ConnectionInfo.UserID = strUserID;

                tLogOnInfo.ConnectionInfo.Password = strPassword;

                cTable.ApplyLogOnInfo(tLogOnInfo);

            }

        }

        catch (Exception ex)

        { }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#