加载水晶报表
加载水晶报表有两种方法,这是其中一种.
private Framework.Win.Library.Database _db = null;
private CrystalDecisions.CrystalReports.Engine.ReportDocument rptDOC;
private bool printReport()
{
DataView dv;
string strRepVer = "A";
dv = GetReportHd(Convert.ToInt32(cmbReportName.SelectedValue.ToString()));
strRepVer = dv[0]["Version"].ToString();
string strReportName = dv[0]["RepPath"].ToString() + dv[0]["RepID"].ToString();
//string strReportName = "W:\\WGERP\\Reports\\WGERP004_test.rpt";
rptDOC = new ReportDocument();
try
{
rptDOC.Load(strReportName);
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = _db.Server;
connectionInfo.DatabaseName = _db.DatabaseName;
connectionInfo.UserID = _db.UserID;
connectionInfo.Password = _db.Password;
SetDBLogonForReport(connectionInfo, rptDOC);
SetDBLogonForSubreports(connectionInfo, rptDOC);
string strBeginStyleNo = "";
string strEndStyleNo = "";
DateTime BeginDate = dtpBeginDate.Value.Date;
DateTime EndDate = dtpEndDate.Value.Date;
strBeginStyleNo = txtSO.Text.Trim().ToString();
strEndStyleNo = txtStyleEndNo.Text.Trim().ToString();
if (cmbReportName.Text.Trim() == "每日车间流水表-A")
{
SetReportParameter(rptDOC, "@Fr_MO", cTextBox1.Text.Trim().ToString());
SetReportParameter(rptDOC, "@To_MO", cTextBox2.Text.Trim().ToString());
SetReportParameter(rptDOC, "@Fr_Day", dtp1.Value.Date.ToString());
}
else if (cmbReportName.Text.Trim() == "款式价格-A")
{
SetReportParameter(rptDOC, "@beginStyle_NO", strBeginStyleNo);
SetReportParameter(rptDOC, "@endStyle_No", strEndStyleNo);
//SetReportParameter(rptDOC, "@begindate", BeginDate.ToString());
//SetReportParameter(rptDOC, "@enddate", BeginDate.ToString());
}
else
{
SetReportParameter(rptDOC, "@beginStyle_NO", strBeginStyleNo);
SetReportParameter(rptDOC, "@endStyle_No", strEndStyleNo);
}
}
catch(Exception ex)
{
_db.Disconnect();
MessageBox.Show(ex.Message.ToString());
}
finally
{
_db.Disconnect();
}
return true;
}
private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
private void SetDBLogonForSubreports(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo, subReportDocument);
}
}
}
}
private void SetReportParameter(ReportDocument rptDoc, string strPara, string strParaValue)
{
ParameterValues currentParameterValues = new ParameterValues();
ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
parameterDiscreteValue.Value = strParaValue.ToString();
currentParameterValues.Add(parameterDiscreteValue);
ParameterFieldDefinitions parameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[strPara];
parameterFieldDefinition.ApplyCurrentValues(currentParameterValues);
}
private Framework.Win.Library.Database _db = null;
private CrystalDecisions.CrystalReports.Engine.ReportDocument rptDOC;
private bool printReport()
{
DataView dv;
string strRepVer = "A";
dv = GetReportHd(Convert.ToInt32(cmbReportName.SelectedValue.ToString()));
strRepVer = dv[0]["Version"].ToString();
string strReportName = dv[0]["RepPath"].ToString() + dv[0]["RepID"].ToString();
//string strReportName = "W:\\WGERP\\Reports\\WGERP004_test.rpt";
rptDOC = new ReportDocument();
try
{
rptDOC.Load(strReportName);
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = _db.Server;
connectionInfo.DatabaseName = _db.DatabaseName;
connectionInfo.UserID = _db.UserID;
connectionInfo.Password = _db.Password;
SetDBLogonForReport(connectionInfo, rptDOC);
SetDBLogonForSubreports(connectionInfo, rptDOC);
string strBeginStyleNo = "";
string strEndStyleNo = "";
DateTime BeginDate = dtpBeginDate.Value.Date;
DateTime EndDate = dtpEndDate.Value.Date;
strBeginStyleNo = txtSO.Text.Trim().ToString();
strEndStyleNo = txtStyleEndNo.Text.Trim().ToString();
if (cmbReportName.Text.Trim() == "每日车间流水表-A")
{
SetReportParameter(rptDOC, "@Fr_MO", cTextBox1.Text.Trim().ToString());
SetReportParameter(rptDOC, "@To_MO", cTextBox2.Text.Trim().ToString());
SetReportParameter(rptDOC, "@Fr_Day", dtp1.Value.Date.ToString());
}
else if (cmbReportName.Text.Trim() == "款式价格-A")
{
SetReportParameter(rptDOC, "@beginStyle_NO", strBeginStyleNo);
SetReportParameter(rptDOC, "@endStyle_No", strEndStyleNo);
//SetReportParameter(rptDOC, "@begindate", BeginDate.ToString());
//SetReportParameter(rptDOC, "@enddate", BeginDate.ToString());
}
else
{
SetReportParameter(rptDOC, "@beginStyle_NO", strBeginStyleNo);
SetReportParameter(rptDOC, "@endStyle_No", strEndStyleNo);
}
}
catch(Exception ex)
{
_db.Disconnect();
MessageBox.Show(ex.Message.ToString());
}
finally
{
_db.Disconnect();
}
return true;
}
private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Tables tables = reportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}
private void SetDBLogonForSubreports(ConnectionInfo connectionInfo, ReportDocument reportDocument)
{
Sections sections = reportDocument.ReportDefinition.Sections;
foreach (Section section in sections)
{
ReportObjects reportObjects = section.ReportObjects;
foreach (ReportObject reportObject in reportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject = (SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo, subReportDocument);
}
}
}
}
private void SetReportParameter(ReportDocument rptDoc, string strPara, string strParaValue)
{
ParameterValues currentParameterValues = new ParameterValues();
ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
parameterDiscreteValue.Value = strParaValue.ToString();
currentParameterValues.Add(parameterDiscreteValue);
ParameterFieldDefinitions parameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
ParameterFieldDefinition parameterFieldDefinition = parameterFieldDefinitions[strPara];
parameterFieldDefinition.ApplyCurrentValues(currentParameterValues);
}
海阔天高