4,816
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace WebReport
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{
private ReportDocument crReportDocument;
private ParameterFields crParameterFields;
private ParameterField crParameterField;
private ParameterValues crParameterValues;
private ParameterDiscreteValue crParameterDiscreteValue;
protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
if (!IsPostBack)
{
TxtDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
TxtDate2.Text = DateTime.Now.ToString("yyyy-MM-dd");
// Initialize Dropdownlist
string reportPath = Server.MapPath("Reports/");
string[] reports = Directory.GetFiles(reportPath, "*.rpt");
IDictionary sortedList = new SortedList();
foreach (string path in reports)
{
crReportDocument = new ReportDocument();
crReportDocument.Load(path);
string reportName = crReportDocument.SummaryInfo.ReportTitle;
if (reportName == null || reportName.Length == 0)
{
int reportNamePrefix = path.LastIndexOf(@"\") + 1;
int reportNameLength = path.Length - reportNamePrefix;
reportName = path.Substring(reportNamePrefix, reportNameLength);
}
sortedList.Add(path, reportName);
crReportDocument = null;
}
reportsList.DataTextField = "value";
reportsList.DataValueField = "key";
reportsList.DataSource = sortedList;
reportsList.DataBind();
ConfigureCrystalReports();
}
else
{
TxtDate1.Text = (string)Session["dateStart"];
TxtDate2.Text = (string)Session["dateEnd"];
crReportDocument = (ReportDocument)Session["reportDocument"];
CrystalReportViewer1.ReportSource = crReportDocument;
}
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
protected void Button1_Click(object sender, System.EventArgs e)
{
ConfigureCrystalReports();
Session["reportDocument"] = crReportDocument;
Session["dateStart"] = TxtDate1.Text;
Session["dateEnd"] = TxtDate2.Text;
}
private void ConfigureCrystalReports()
{
//Create an instance of the strongly-typed report object
crReportDocument = new ReportDocument();
crReportDocument.Load(reportsList.SelectedValue);
LogOnDB();
CrystalReportViewer1.ReportSource = crReportDocument;
//Get the collection of parameters from the report
crParameterFields = CrystalReportViewer1.ParameterFieldInfo;
//Access the specified parameter from the collection
crParameterField = crParameterFields["dateStart"];
//Get the current values from the parameter field. At this point
//there are zero values set.
crParameterValues = crParameterField.CurrentValues;
crParameterValues.Clear();
//Set the current values for the parameter field
crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = Convert.ToDateTime(TxtDate1.Text + " 00:00:00");
//Add the current value for the parameter field
crParameterValues.Add(crParameterDiscreteValue);
crParameterDiscreteValue = null;
//Access the specified parameter from the collection
crParameterField = crParameterFields["dateEnd"];
//Get the current values from the parameter field. At this point
//there are zero values set.
crParameterValues = crParameterField.CurrentValues;
//Set the current values for the parameter field
crParameterDiscreteValue = new ParameterDiscreteValue();
crParameterDiscreteValue.Value = Convert.ToDateTime(TxtDate2.Text + " 23:59:59");
//Add the current value for the parameter field
crParameterValues.Add(crParameterDiscreteValue);
//Set the modified parameters collection back to the viewer so that
//the new parameter information can be used for the report.
CrystalReportViewer1.ParameterFieldInfo = crParameterFields;
}
private void LogOnDB ()
{
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
ConnectionInfo crConnectionInfo;
//从web.config中获取logOnInfo参数信息
string DatabaseName = System.Configuration.ConfigurationManager.AppSettings["DatabaseName"];
string ServerName = System.Configuration.ConfigurationManager.AppSettings["ServerName"];
string UserID = System.Configuration.ConfigurationManager.AppSettings["UserID"];
string Password = System.Configuration.ConfigurationManager.AppSettings["Password"];
//string SchemaName = System.Configuration.ConfigurationManager.AppSettings["SchemaName"];
//Set the CrTables object to the Tables collection of the Report's dDtabase
crTables = crReportDocument.Database.Tables;
//Loop through each Table object in the Tables collection and apply the logon info
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
//Set the crConnectionInfo with the current values stored in the report
crConnectionInfo = crTable.LogOnInfo.ConnectionInfo;
//Populate the ConnectionInfo Objects Properties with the appropriate values for
//the ServerName,User ID, Password and DatabaseName.
//However, since Oracle works on Schemas, Crystal Reports does not recognize or store a DatabaseName.
//Therefore, the DatabaseName property must be set to a BLANK string.
crConnectionInfo.DatabaseName = DatabaseName;
crConnectionInfo.ServerName = ServerName;
crConnectionInfo.UserID = UserID;
crConnectionInfo.Password = Password;
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
//若要改变方案名字,请按下列格式设定
//crTable.Location = "<new schema name>." + crTable.Name;
//crTable.Location = SchemaName + "." + crTable.Name;
}
}
}
}