您的位置:首页 > 其它

水晶报表中动态加载报表字段

2006-12-17 23:36 465 查看
using System;

using System.Collections;

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.HtmlControls;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

using System.Globalization;

namespace WEB2003Test.CryStalReportTest

{

/// <summary>

/// DynamicReportCommon 的摘要说明。

/// </summary>

public class DynamicReportCommon : System.Web.UI.Page

{

protected CrystalDecisions.Web.CrystalReportViewer crvEmployees;

protected DataSet ds;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

try

{

if(!Page.IsPostBack)

{

if(Session["DynamicReportData"]!=null)

{

ds = (DataSet)Session["DynamicReportData"];

Session.Remove("DynamicReportData"); //释放Session中的值

Page.Cache["Data"]=ds; //页面缓存报表导出打印时重新绑定报表用

string tableName = ds.Tables[0].TableName; //DataSet表名

string [] strDescription = new string[ds.Tables[0].Columns.Count]; //存放标头的字符串数

this.GetTableNameAndeColName(ds,tableName,strDescription); //获取表名及列名

CrystalDecisions.CrystalReports.Engine.ReportDocument cryDocument = new TextDynamicReport();

this.NoLineBoundReport(ds,strDescription,tableName,cryDocument,this.crvEmployees,18);

}

else

{

if(Page.Request.QueryString["ReturnUrl"]!=null)

{

Response.Redirect(Page.Request.QueryString["ReturnUrl"].ToString());

}

else

{

this.PrintStr("没有数据信息,报表加载失败!");

}

}

}

else //这一部分应该自定义

{

ds = (DataSet)Page.Cache["Data"];

string tableName = ds.Tables[0].TableName; //DataSet表名

string [] strDescription = new string[ds.Tables[0].Columns.Count]; //存放标头的字符串数

this.GetTableNameAndeColName(ds,tableName,strDescription); //获取表名及列名

CrystalDecisions.CrystalReports.Engine.ReportDocument cryDocument = new TextDynamicReport();

this.NoLineBoundReport(ds,strDescription,tableName,cryDocument,this.crvEmployees,18);

}

}

catch(Exception ee)

{

this.PrintStr(ee.Message);

Server.ClearError();

}

}

/// <summary>

/// 报表绑定

/// </summary>

/// <param name="ds">要绑定的数据集</param>

/// <param name="titleArray">要绑定的字段</param>

/// <param name="tableName">绑定的表名</param></param>

/// <param name="cryDocument">绑定的报表对象</param>

/// <param name="cryDocument">显示的报表对象</param>

/// <param name="cryDocument">合计的报表列数(注所有列的下划线条从Line11开始递增命名) 行的下划线以(Line1、Line2、Line3命名)</param>

private void NoLineBoundReport(DataSet ds, string [] titleArray, string tableName,ReportDocument cryDocument,CrystalDecisions.Web.CrystalReportViewer crvEmployees,int totalColoums)

{

try

{

CrystalDecisions.Shared.ParameterFields parFields = new ParameterFields(); //参数字段集合

CrystalDecisions.Shared.ParameterField parField; //参数字段

CrystalDecisions.Shared.ParameterDiscreteValue parDescrete; //用于参数字段付值

CrystalDecisions.CrystalReports.Engine.LineObject line; //用于报表Line对像

CrystalDecisions.CrystalReports.Engine.TextObject textBox; //用于报表TextBox

int [] coloumLength = new int[ds.Tables[tableName].Columns.Count]; //存放参数及公式字段长度的数组

int intPostions =0;

DataTable dataTable = FillDataTableForDataSet(ds,tableName,titleArray,coloumLength); //这部分要改进

this.GetPosition(coloumLength);

for(int i=0; i<titleArray.Length; i++) //设置字段值

{

textBox = cryDocument.ReportDefinition.ReportObjects["txt" + (i+1)] as TextObject;

textBox.Text = " " + titleArray[i];

cryDocument.DataDefinition.FormulaFields["Formula" + (i+1)].Text="{" + tableName + "." +titleArray[i].Trim() + "}";

}

parField = new ParameterField();

parField.ParameterFieldName = "ReportTitle";

parDescrete = new ParameterDiscreteValue();

parDescrete.Value = tableName;

parField.CurrentValues.Add(parDescrete);

parField.AllowCustomValues = false;

parFields.Add(parField);

crvEmployees.ParameterFieldInfo = parFields;

intPostions =0;

for(int i=0; i<titleArray.Length; i++) //设置报表对像位置

{

if(i==0)

{

cryDocument.ReportDefinition.ReportObjects["Formula" + (i+1)].Left=30;

intPostions=30;

textBox = cryDocument.ReportDefinition.ReportObjects["txt" + (i+1)] as TextObject;

textBox.Left=30;

textBox.Border.RightLineStyle = LineStyle.SingleLine;

textBox.Width = coloumLength[i];

}

else

{

intPostions += coloumLength[i-1];

cryDocument.ReportDefinition.ReportObjects["Formula" + (i+1)].Left=intPostions;

textBox = cryDocument.ReportDefinition.ReportObjects["txt" + (i+1)] as TextObject;

textBox.Left=intPostions;

textBox.Border.RightLineStyle = LineStyle.SingleLine;

textBox.Width = coloumLength[i];

}

cryDocument.ReportDefinition.ReportObjects["Formula" + (i+1)].Width=coloumLength[i];

cryDocument.ReportDefinition.ReportObjects["Formula" + (i+1)].Border.LeftLineStyle=LineStyle.NoLine;

cryDocument.ReportDefinition.ReportObjects["Formula" + (i+1)].Border.TopLineStyle=LineStyle.NoLine;

cryDocument.ReportDefinition.ReportObjects["Formula" + (i+1)].Border.BottomLineStyle=LineStyle.NoLine;

}

intPostions += coloumLength[coloumLength.Length-1];

for(int i=1;i<4;i++)

{

line = cryDocument.ReportDefinition.ReportObjects["Line"+i] as LineObject;

line.Right = intPostions;

}

cryDocument.SetDataSource(dataTable);

crvEmployees.ReportSource = cryDocument;

crvEmployees.DisplayGroupTree = true;

crvEmployees.DataBind();

ds.Dispose();

}

catch(Exception ee)

{

this.PrintStr(@""+ ee.Message+"");

Server.ClearError();

}

}

/// <summary>

/// 自动将DataSet中的数据转换成字符串同进获取其数据类型的长度

/// </summary>

/// <param name="ds">数据源DataSet</param>

/// <param name="tableName">表名</param>

/// <param name="columnsName">列名数组</param>

/// <param name="columnsLength">输出参数列长度数组</param>

/// <returns>DataTable</returns>

private DataTable FillDataTableForDataSet(DataSet ds,string tableName,string [] columnsName,int []columnsLength)

{

if(ds.Tables[tableName].Columns.Count!=columnsName.Length||columnsName.Length!=columnsLength.Length||ds.Tables[tableName].Columns.Count!=columnsLength.Length) //传入的参数长度不一致时返回空值

{

return null;

}

DataTable dataTable = new DataTable(tableName);

System.Data.DataColumn column; //要新建的表列对象

System.Data.DataRow row;

for(int i=0; i<columnsName.Length; i++) //创建数据表

{

column = new DataColumn(columnsName[i].Trim(),System.Type.GetType("System.String"));

columnsLength[i] = ds.Tables[tableName].Columns[columnsName[i]].MaxLength;

dataTable.Columns.Add(column);

}

for(int i=0; i<ds.Tables[tableName].Rows.Count; i++)

{

row = dataTable.NewRow();

for(int j=0; j<columnsName.Length; j++)

{

if(ds.Tables[tableName].Rows[i][columnsName[j]].GetType().FullName == "System.DateTime")

{

row[columnsName[j]] = this.GetStringForDataTime(ds.Tables[tableName].Rows[i][columnsName[j]]);//将时间类型的转换为为-06-05这样的格式

}

else if(ds.Tables[tableName].Rows[i][columnsName[j]].GetType().FullName == "System.Double")

{

row[columnsName[j]] = this.GetStringForDouble(ds.Tables[tableName].Rows[i][columnsName[j]]);//将小数转换为只带两位小数的格式

}

else if(ds.Tables[tableName].Rows[i][columnsName[j]].GetType().FullName == "System.DBNull")

{

row[columnsName[j]] = " "+((char)6);

}

else

{

row[columnsName[j]] = this.Getstring(ds.Tables[tableName].Rows[i][columnsName[j]]);

}

//row[columnsName[j]] =ds.Tables[tableName].Rows[i][columnsName[j]];

if(columnsLength[j]<row[columnsName[j]].ToString().Length)

{

columnsLength[j]=row[columnsName[j]].ToString().Length;

}

}

dataTable.Rows.Add(row);

}

return dataTable;

}

/// <summary>

/// 将日期类型转化为-06-12形式的字符串

/// </summary>

/// <param name="var">要转换的对象</param>

/// <returns返回的字符串</returns>

private string GetStringForDataTime(object var)

{

try

{

return "" + Convert.ToDateTime(var).ToString("yyyy-MM-dd");

}

catch

{

return "";

}

}

/// <summary>

/// 将Double类型的数据转化成两们小数的字符串

/// </summary>

/// <param name="var">要转换的字符串</param>

/// <returns>返回的字符串</returns>

private string GetStringForDouble(object var)

{

System.Globalization.NumberFormatInfo doubleNfi = new CultureInfo("en-US",false).NumberFormat; //小数字符串保留两位小数

doubleNfi.NumberDecimalDigits = 2;

try

{

return "" + Convert.ToString(var).ToString(doubleNfi);

}

catch

{

return "";

}

}

/// <summary>

/// 字符串转换

/// </summary>

/// <param name="var">要装换的对象</param>

/// <returns>返回的字符串</returns>

private string Getstring(object var)

{

try

{

return "" + Convert.ToString(var);

}

catch

{

return "";

}

}

/// <summary>

/// 将Double型字符串转换成Int型字符串

/// </summary>

/// <param name="var"></param>

/// <returns></returns>

private int GetIntForDouble(object var)

{

try

{

return Convert.ToInt32(var);

}

catch

{

return 0;

}

}

/// <summary>

/// 获取报表对象启始坐标位置

/// </summary>

/// <param name="coloumLength"></param>

private void GetPosition(int [] coloumLength)

{

int totalLength = 0;

int [] temp = new int[coloumLength.Length];

for(int i=0; i<coloumLength.Length; i++)

{

totalLength +=coloumLength[i];

temp[i]=coloumLength[i];

}

if(totalLength==0)

{

return;

}

for(int i=0; i<coloumLength.Length; i++)

{

coloumLength[i] = this.GetIntForDouble(temp[i]*11260/totalLength);

}

}

/// <summary>

/// 弹出提示窗口

/// </summary>

/// <param name="Message">提示信息</param>

private void PrintStr(string Message)

{

System.Text.RegularExpressions.Regex digitregex = new System.Text.RegularExpressions.Regex("[']");

Message = digitregex.Replace(Message,"’");

Page.RegisterStartupScript(new Random().Next(1000000).ToString(), "<script>alert('" + Message + "');</script>");

}

/// <summary>

/// 根据DataSet获取表名及字段名数组

/// </summary>

/// <param name="ds"></param>

/// <param name="tableName"></param>

/// <param name="columnsNames"></param>

private void GetTableNameAndeColName(DataSet ds,string tableName,string[]columnsNames)

{

for(int i=0; i<ds.Tables[tableName].Columns.Count; i++)

{

columnsNames[i] = ds.Tables[tableName].Columns[i].ColumnName.ToString();

}

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// <summary>

/// 设计器支持所需的方法- 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: