1. 程式人生 > >C#操作水晶報表跟轉換成PDF格式

C#操作水晶報表跟轉換成PDF格式

           這邊是通過C#將引數帶入到水晶報表中,生成對應的報表的程式碼,跟將水晶報表匯出轉成PDF格式儲存

這邊是將引數匯入到水晶報表中的方法

public void BindReports(引數1, 引數2)
        {
            CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();
            ReportDocument crvReport = new ReportDocument();
            string reportFileName = null;
            string reportFilePath = null;
            reportFilePath = System.Windows.Forms.Application.StartupPath + "\\report\\Shipment.rpt";
             reportFileName = "1122334";//這邊是匯出後的檔名稱
             crvReport.Load(reportFilePath);

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
            ConnectionInfo myConnInfo = new ConnectionInfo();
            myConnInfo.DatabaseName = "你將表放到的那個庫的名稱";
            myConnInfo.UserID = "sa";//SQL SERVER賬號
            myConnInfo.Password = "sa.";//SQL SERVER密碼
            myConnInfo.ServerName = con.DataSource;

            CrystalReportViewer1.ReportSource = crvReport;

            this.SetDBLogonForReport(myConnInfo, crvReport);

            //這邊是第一個引數的設定
            ParameterFieldDefinitions FOTPerformanceReportParameterFieldDefinitions1 = crvReport.DataDefinition.ParameterFields;
            ParameterFieldDefinition plantCrystalPara1 = FOTPerformanceReportParameterFieldDefinitions1["@水晶報表中引數1的名稱"];
            ParameterDiscreteValue crvReportParameterDiscreteValue1 = new ParameterDiscreteValue();
            ParameterValues currentParameterValues1 = new ParameterValues();
            crvReportParameterDiscreteValue1.Value = 引數1;
            currentParameterValues1.Add(crvReportParameterDiscreteValue1);
            plantCrystalPara1.ApplyCurrentValues(currentParameterValues1);

            ParameterFieldDefinitions FOTPerformanceReportParameterFieldDefinitions2 = crvReport.DataDefinition.ParameterFields;
            ParameterFieldDefinition plantCrystalPara2 = FOTPerformanceReportParameterFieldDefinitions2["@水晶報表中差數2的名稱"];
            ParameterDiscreteValue crvReportParameterDiscreteValue2 = new ParameterDiscreteValue();
            ParameterValues currentParameterValues2 = new ParameterValues();
            crvReportParameterDiscreteValue2.Value = 引數2;
            currentParameterValues2.Add(crvReportParameterDiscreteValue2);
            plantCrystalPara2.ApplyCurrentValues(currentParameterValues2);
            //直接開啟
            // this.CrystalReportViewer1.ReportSource = crvReport;
            ExportReport(crvReport, reportFileName);
        }
        private void SetDBLogonForReport(ConnectionInfo reportConnectionInfo, ReportDocument mjsReport)
        {
            Tables mjsReportTables = mjsReport.Database.Tables;

            foreach (CrystalDecisions.CrystalReports.Engine.Table mjsTable in mjsReportTables)
            {
                TableLogOnInfo mjsTableLogonInfo = mjsTable.LogOnInfo;
                mjsTableLogonInfo.ConnectionInfo = reportConnectionInfo;
                mjsTable.ApplyLogOnInfo(mjsTableLogonInfo);
            }

        }
這邊是匯出PDF的方法
        //匯出
        private void ExportReport(ReportDocument mjsReport, string reportFileName)
        {
            SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            ExportOptions mjsReportExportOptions = new ExportOptions();

            mjsReportExportOptions = mjsReport.ExportOptions;

            mjsReportExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
            SaveFileDialog1.Filter = "Report Files (*.pdf)|*.pdf";
            SaveFileDialog1.FileName = reportFileName;
            if (SaveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                reportFileName = SaveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            if (!string.IsNullOrEmpty(reportFileName))
            {
                mjsReport.ExportToDisk(ExportFormatType.PortableDocFormat, reportFileName);
                Process.Start((reportFileName));
                //MessageBox.Show("匯出成功!", "檔案路徑:" + reportFileName);
            }

        }

在你的程式中,只需要像

BindReports(引數1,引數2);

這樣呼叫就可直接將引數帶入到水晶報表並且轉換成PDF格式