这边是通过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);
}
}
//导出
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格式