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>
publicclassDynamicReportCommon:System.Web.UI.Page
...{
protectedCrystalDecisions.Web.CrystalReportViewercrvEmployees;
protectedDataSetds;
privatevoidPage_Load(objectsender,System.EventArgse)
...{
//在此处放置用户代码以初始化页面
try
...{
if(!Page.IsPostBack)
...{
if(Session["DynamicReportData"]!=null)
...{
ds=(DataSet)Session["DynamicReportData"];
Session.Remove("DynamicReportData");//释放Session中的值
Page.Cache["Data"]=ds;//页面缓存报表导出打印时重新绑定报表用
stringtableName=ds.Tables[0].TableName;//DataSet表名
string[]strDescription=newstring[ds.Tables[0].Columns.Count];//存放标头的字符串数
this.GetTableNameAndeColName(ds,tableName,strDescription);//获取表名及列名
CrystalDecisions.CrystalReports.Engine.ReportDocumentcryDocument=newTextDynamicReport();
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"];
stringtableName=ds.Tables[0].TableName;//DataSet表名
string[]strDescription=newstring[ds.Tables[0].Columns.Count];//存放标头的字符串数
this.GetTableNameAndeColName(ds,tableName,strDescription);//获取表名及列名
CrystalDecisions.CrystalReports.Engine.ReportDocumentcryDocument=newTextDynamicReport();
this.NoLineBoundReport(ds,strDescription,tableName,cryDocument,this.crvEmployees,18);
}
}
catch(Exceptionee)
...{
this.PrintStr(ee.Message);
Server.ClearError();
}
}
/**////<summary>
///报表绑定
///</summary>
///<paramname="ds">要绑定的数据集</param>
///<paramname="titleArray">要绑定的字段</param>
///<paramname="tableName">绑定的表名</param></param>
///<paramname="cryDocument">绑定的报表对象</param>
///<paramname="cryDocument">显示的报表对象</param>
///<paramname="cryDocument">合计的报表列数(注所有列的下划线条从Line11开始递增命名) 行的下划线以(Line1、Line2、Line3命名)</param>
privatevoidNoLineBoundReport(DataSetds,string[]titleArray,stringtableName,ReportDocumentcryDocument,CrystalDecisions.Web.CrystalReportViewercrvEmployees,inttotalColoums)
...{
try
...{
CrystalDecisions.Shared.ParameterFieldsparFields=newParameterFields();//参数字段集合
CrystalDecisions.Shared.ParameterFieldparField;//参数字段
CrystalDecisions.Shared.ParameterDiscreteValueparDescrete;//用于参数字段付值
CrystalDecisions.CrystalReports.Engine.LineObjectline;//用于报表Line对像
CrystalDecisions.CrystalReports.Engine.TextObjecttextBox;//用于报表TextBox
int[]coloumLength=newint[ds.Tables[tableName].Columns.Count];//存放参数及公式字段长度的数组
intintPostions=0;
DataTabledataTable=FillDataTableForDataSet(ds,tableName,titleArray,coloumLength);//这部分要改进
this.GetPosition(coloumLength);
for(inti=0;i<titleArray.Length;i++)//设置字段值
...{
textBox=cryDocument.ReportDefinition.ReportObjects["txt"+(i+1)]asTextObject;
textBox.Text=""+titleArray[i];
cryDocument.DataDefinition.FormulaFields["Formula"+(i+1)].Text="{"+tableName+"."+titleArray[i].Trim()+"}";
}
parField=newParameterField();
parField.ParameterFieldName="ReportTitle";
parDescrete=newParameterDiscreteValue();
parDescrete.Value=tableName;
parField.CurrentValues.Add(parDescrete);
parField.AllowCustomValues=false;
parFields.Add(parField);
crvEmployees.ParameterFieldInfo=parFields;
intPostions=0;
for(inti=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)]asTextObject;
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)]asTextObject;
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(inti=1;i<4;i++)
...{
line=cryDocument.ReportDefinition.ReportObjects["Line"+i]asLineObject;
line.Right=intPostions;
}
cryDocument.SetDataSource(dataTable);
crvEmployees.ReportSource=cryDocument;
crvEmployees.DisplayGroupTree=true;
crvEmployees.DataBind();
ds.Dispose();
}
catch(Exceptionee)
...{
this.PrintStr(@""+ee.Message+"");
Server.ClearError();
}
}
/**////<summary>
///自动将DataSet中的数据转换成字符串同进获取其数据类型的长度
///</summary>
///<paramname="ds">数据源DataSet</param>
///<paramname="tableName">表名</param>
///<paramname="columnsName">列名数组</param>
///<paramname="columnsLength">输出参数列长度数组</param>
///<returns>DataTable</returns>
privateDataTableFillDataTableForDataSet(DataSetds,stringtableName,string[]columnsName,int[]columnsLength)
...{
if(ds.Tables[tableName].Columns.Count!=columnsName.Length||columnsName.Length!=columnsLength.Length||ds.Tables[tableName].Columns.Count!=columnsLength.Length)//传入的参数长度不一致时返回空值
...{
returnnull;
}
DataTabledataTable=newDataTable(tableName);
System.Data.DataColumncolumn;//要新建的表列对象
System.Data.DataRowrow;
for(inti=0;i<columnsName.Length;i++)//创建数据表
...{
column=newDataColumn(columnsName[i].Trim(),System.Type.GetType("System.String"));
columnsLength[i]=ds.Tables[tableName].Columns[columnsName[i]].MaxLength;
dataTable.Columns.Add(column);
}
for(inti=0;i<ds.Tables[tableName].Rows.Count;i++)
...{
row=dataTable.NewRow();
for(intj=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这样的格式
}
elseif(ds.Tables[tableName].Rows[i][columnsName[j]].GetType().FullName=="System.Double")
...{
row[columnsName[j]]=this.GetStringForDouble(ds.Tables[tableName].Rows[i][columnsName[j]]);//将小数转换为只带两位小数的格式
}
elseif(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);
}
returndataTable;
}
/**////<summary>
///将日期类型转化为-06-12形式的字符串
///</summary>
///<paramname="var">要转换的对象</param>
///<returns返回的字符串</returns>
privatestringGetStringForDataTime(objectvar)
...{
try
...{
return""+Convert.ToDateTime(var).ToString("yyyy-MM-dd");
}
catch
...{
return"";
}
}
/**////<summary>
///将Double类型的数据转化成两们小数的字符串
///</summary>
///<paramname="var">要转换的字符串</param>
///<returns>返回的字符串</returns>
privatestringGetStringForDouble(objectvar)
...{
System.Globalization.NumberFormatInfodoubleNfi=newCultureInfo("en-US",false).NumberFormat;//小数字符串保留两位小数
doubleNfi.NumberDecimalDigits=2;
try
...{
return""+Convert.ToString(var).ToString(doubleNfi);
}
catch
...{
return"";
}
}
/**////<summary>
///字符串转换
///</summary>
///<paramname="var">要装换的对象</param>
///<returns>返回的字符串</returns>
privatestringGetstring(objectvar)
...{
try
...{
return""+Convert.ToString(var);
}
catch
...{
return"";
}
}
/**////<summary>
///将Double型字符串转换成Int型字符串
///</summary>
///<paramname="var"></param>
///<returns></returns>
privateintGetIntForDouble(objectvar)
...{
try
...{
returnConvert.ToInt32(var);
}
catch
...{
return0;
}
}
/**////<summary>
///获取报表对象启始坐标位置
///</summary>
///<paramname="coloumLength"></param>
privatevoidGetPosition(int[]coloumLength)
...{
inttotalLength=0;
int[]temp=newint[coloumLength.Length];
for(inti=0;i<coloumLength.Length;i++)
...{
totalLength+=coloumLength[i];
temp[i]=coloumLength[i];
}
if(totalLength==0)
...{
return;
}
for(inti=0;i<coloumLength.Length;i++)
...{
coloumLength[i]=this.GetIntForDouble(temp[i]*11260/totalLength);
}
}
/**////<summary>
///弹出提示窗口
///</summary>
///<paramname="Message">提示信息</param>
privatevoidPrintStr(stringMessage)
...{
System.Text.RegularExpressions.Regexdigitregex=newSystem.Text.RegularExpressions.Regex("[']");
Message=digitregex.Replace(Message,"’");
Page.RegisterStartupScript(newRandom().Next(1000000).ToString(),"<script>alert('"+Message+"');</script>");
}
/**////<summary>
///根据DataSet获取表名及字段名数组
///</summary>
///<paramname="ds"></param>
///<paramname="tableName"></param>
///<paramname="columnsNames"></param>
privatevoidGetTableNameAndeColName(DataSetds,stringtableName,string[]columnsNames)
...{
for(inti=0;i<ds.Tables[tableName].Columns.Count;i++)
...{
columnsNames[i]=ds.Tables[tableName].Columns[i].ColumnName.ToString();
}
}
Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
...{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
...{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}
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>
publicclassDynamicReportCommon:System.Web.UI.Page
...{
protectedCrystalDecisions.Web.CrystalReportViewercrvEmployees;
protectedDataSetds;
privatevoidPage_Load(objectsender,System.EventArgse)
...{
//在此处放置用户代码以初始化页面
try
...{
if(!Page.IsPostBack)
...{
if(Session["DynamicReportData"]!=null)
...{
ds=(DataSet)Session["DynamicReportData"];
Session.Remove("DynamicReportData");//释放Session中的值
Page.Cache["Data"]=ds;//页面缓存报表导出打印时重新绑定报表用
stringtableName=ds.Tables[0].TableName;//DataSet表名
string[]strDescription=newstring[ds.Tables[0].Columns.Count];//存放标头的字符串数
this.GetTableNameAndeColName(ds,tableName,strDescription);//获取表名及列名
CrystalDecisions.CrystalReports.Engine.ReportDocumentcryDocument=newTextDynamicReport();
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"];
stringtableName=ds.Tables[0].TableName;//DataSet表名
string[]strDescription=newstring[ds.Tables[0].Columns.Count];//存放标头的字符串数
this.GetTableNameAndeColName(ds,tableName,strDescription);//获取表名及列名
CrystalDecisions.CrystalReports.Engine.ReportDocumentcryDocument=newTextDynamicReport();
this.NoLineBoundReport(ds,strDescription,tableName,cryDocument,this.crvEmployees,18);
}
}
catch(Exceptionee)
...{
this.PrintStr(ee.Message);
Server.ClearError();
}
}
/**////<summary>
///报表绑定
///</summary>
///<paramname="ds">要绑定的数据集</param>
///<paramname="titleArray">要绑定的字段</param>
///<paramname="tableName">绑定的表名</param></param>
///<paramname="cryDocument">绑定的报表对象</param>
///<paramname="cryDocument">显示的报表对象</param>
///<paramname="cryDocument">合计的报表列数(注所有列的下划线条从Line11开始递增命名) 行的下划线以(Line1、Line2、Line3命名)</param>
privatevoidNoLineBoundReport(DataSetds,string[]titleArray,stringtableName,ReportDocumentcryDocument,CrystalDecisions.Web.CrystalReportViewercrvEmployees,inttotalColoums)
...{
try
...{
CrystalDecisions.Shared.ParameterFieldsparFields=newParameterFields();//参数字段集合
CrystalDecisions.Shared.ParameterFieldparField;//参数字段
CrystalDecisions.Shared.ParameterDiscreteValueparDescrete;//用于参数字段付值
CrystalDecisions.CrystalReports.Engine.LineObjectline;//用于报表Line对像
CrystalDecisions.CrystalReports.Engine.TextObjecttextBox;//用于报表TextBox
int[]coloumLength=newint[ds.Tables[tableName].Columns.Count];//存放参数及公式字段长度的数组
intintPostions=0;
DataTabledataTable=FillDataTableForDataSet(ds,tableName,titleArray,coloumLength);//这部分要改进
this.GetPosition(coloumLength);
for(inti=0;i<titleArray.Length;i++)//设置字段值
...{
textBox=cryDocument.ReportDefinition.ReportObjects["txt"+(i+1)]asTextObject;
textBox.Text=""+titleArray[i];
cryDocument.DataDefinition.FormulaFields["Formula"+(i+1)].Text="{"+tableName+"."+titleArray[i].Trim()+"}";
}
parField=newParameterField();
parField.ParameterFieldName="ReportTitle";
parDescrete=newParameterDiscreteValue();
parDescrete.Value=tableName;
parField.CurrentValues.Add(parDescrete);
parField.AllowCustomValues=false;
parFields.Add(parField);
crvEmployees.ParameterFieldInfo=parFields;
intPostions=0;
for(inti=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)]asTextObject;
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)]asTextObject;
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(inti=1;i<4;i++)
...{
line=cryDocument.ReportDefinition.ReportObjects["Line"+i]asLineObject;
line.Right=intPostions;
}
cryDocument.SetDataSource(dataTable);
crvEmployees.ReportSource=cryDocument;
crvEmployees.DisplayGroupTree=true;
crvEmployees.DataBind();
ds.Dispose();
}
catch(Exceptionee)
...{
this.PrintStr(@""+ee.Message+"");
Server.ClearError();
}
}
/**////<summary>
///自动将DataSet中的数据转换成字符串同进获取其数据类型的长度
///</summary>
///<paramname="ds">数据源DataSet</param>
///<paramname="tableName">表名</param>
///<paramname="columnsName">列名数组</param>
///<paramname="columnsLength">输出参数列长度数组</param>
///<returns>DataTable</returns>
privateDataTableFillDataTableForDataSet(DataSetds,stringtableName,string[]columnsName,int[]columnsLength)
...{
if(ds.Tables[tableName].Columns.Count!=columnsName.Length||columnsName.Length!=columnsLength.Length||ds.Tables[tableName].Columns.Count!=columnsLength.Length)//传入的参数长度不一致时返回空值
...{
returnnull;
}
DataTabledataTable=newDataTable(tableName);
System.Data.DataColumncolumn;//要新建的表列对象
System.Data.DataRowrow;
for(inti=0;i<columnsName.Length;i++)//创建数据表
...{
column=newDataColumn(columnsName[i].Trim(),System.Type.GetType("System.String"));
columnsLength[i]=ds.Tables[tableName].Columns[columnsName[i]].MaxLength;
dataTable.Columns.Add(column);
}
for(inti=0;i<ds.Tables[tableName].Rows.Count;i++)
...{
row=dataTable.NewRow();
for(intj=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这样的格式
}
elseif(ds.Tables[tableName].Rows[i][columnsName[j]].GetType().FullName=="System.Double")
...{
row[columnsName[j]]=this.GetStringForDouble(ds.Tables[tableName].Rows[i][columnsName[j]]);//将小数转换为只带两位小数的格式
}
elseif(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);
}
returndataTable;
}
/**////<summary>
///将日期类型转化为-06-12形式的字符串
///</summary>
///<paramname="var">要转换的对象</param>
///<returns返回的字符串</returns>
privatestringGetStringForDataTime(objectvar)
...{
try
...{
return""+Convert.ToDateTime(var).ToString("yyyy-MM-dd");
}
catch
...{
return"";
}
}
/**////<summary>
///将Double类型的数据转化成两们小数的字符串
///</summary>
///<paramname="var">要转换的字符串</param>
///<returns>返回的字符串</returns>
privatestringGetStringForDouble(objectvar)
...{
System.Globalization.NumberFormatInfodoubleNfi=newCultureInfo("en-US",false).NumberFormat;//小数字符串保留两位小数
doubleNfi.NumberDecimalDigits=2;
try
...{
return""+Convert.ToString(var).ToString(doubleNfi);
}
catch
...{
return"";
}
}
/**////<summary>
///字符串转换
///</summary>
///<paramname="var">要装换的对象</param>
///<returns>返回的字符串</returns>
privatestringGetstring(objectvar)
...{
try
...{
return""+Convert.ToString(var);
}
catch
...{
return"";
}
}
/**////<summary>
///将Double型字符串转换成Int型字符串
///</summary>
///<paramname="var"></param>
///<returns></returns>
privateintGetIntForDouble(objectvar)
...{
try
...{
returnConvert.ToInt32(var);
}
catch
...{
return0;
}
}
/**////<summary>
///获取报表对象启始坐标位置
///</summary>
///<paramname="coloumLength"></param>
privatevoidGetPosition(int[]coloumLength)
...{
inttotalLength=0;
int[]temp=newint[coloumLength.Length];
for(inti=0;i<coloumLength.Length;i++)
...{
totalLength+=coloumLength[i];
temp[i]=coloumLength[i];
}
if(totalLength==0)
...{
return;
}
for(inti=0;i<coloumLength.Length;i++)
...{
coloumLength[i]=this.GetIntForDouble(temp[i]*11260/totalLength);
}
}
/**////<summary>
///弹出提示窗口
///</summary>
///<paramname="Message">提示信息</param>
privatevoidPrintStr(stringMessage)
...{
System.Text.RegularExpressions.Regexdigitregex=newSystem.Text.RegularExpressions.Regex("[']");
Message=digitregex.Replace(Message,"’");
Page.RegisterStartupScript(newRandom().Next(1000000).ToString(),"<script>alert('"+Message+"');</script>");
}
/**////<summary>
///根据DataSet获取表名及字段名数组
///</summary>
///<paramname="ds"></param>
///<paramname="tableName"></param>
///<paramname="columnsNames"></param>
privatevoidGetTableNameAndeColName(DataSetds,stringtableName,string[]columnsNames)
...{
for(inti=0;i<ds.Tables[tableName].Columns.Count;i++)
...{
columnsNames[i]=ds.Tables[tableName].Columns[i].ColumnName.ToString();
}
}
Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
...{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
...{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}