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

来源:互联网 发布:在淘宝买日本刀犯法吗 编辑:程序博客网 时间:2024/05/11 19:07

 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)
        { }
    }

原创粉丝点击