📄 showreport.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
public partial class _Default : System.Web.UI.Page
{
private ReportDocument CommonReport;
///<summary>
///根据具体查询得到的ADO.NET数据集配置水晶报表
///</summary>
private void ConfigureCrystalReports()
{
//生成一个报表对象
CommonReport = new ReportDocument();
//报表文件路径
string reportPath = Server.MapPath("CommonReport.rpt");
//根据路径装载指定报表文件,以进行报表配置操作
CommonReport.Load(reportPath);
//从Session中取得数据集,该数据集是从其它页面查询数据库得到后保存在Session中的
DataSet dataSet = (DataSet)Session["DataSet"];
//将数据集中的表格的名称修改为报表文件中连接的数据表的名字,保证两个表格名称一致
dataSet.Tables[0].TableName = CommonReport.Database.Tables[0].Name;
//获取数据集中字段个数,也是报表中要显示的列的个数
int CountOfColumns = dataSet.Tables[0].Columns.Count;
//定义一个字符串数组,在for循环中生成每个公式字段对象的文本(格式是"{tablename.columnname}"),并保存到数组中以备使用
String[] Text4formularFields = new String[CountOfColumns];
for (int index = 0; index < CountOfColumns; index++)
{
Text4formularFields[index] = "{" + CommonReport.Database.Tables[0].Name + "." + dataSet.Tables[0].Columns[index].ColumnName + "}";
}
//获取报表文件的数据定义对象
DataDefinition dataDefinition = CommonReport.DataDefinition;
//获取数据对象中的公式字段集合
FormulaFieldDefinitions formularFields = dataDefinition.FormulaFields;
for (int columnIndex = 0; columnIndex < CountOfColumns; columnIndex++)
{
//将公式字段的空白文本替换为根据当前数据集dataSet具体字段生成的文本
formularFields[columnIndex].Text = Text4formularFields[columnIndex];
//页眉中的文本对象(也就是报表中每列的标题)设置为dataSet的具体字段名
TextObject CurrentText = (TextObject)(CommonReport.ReportDefinition.Sections["Section2"].ReportObjects[columnIndex]);
CurrentText.Text = dataSet.Tables[0].Columns[columnIndex].ColumnName;
}
//设置报表对象的数据源为dataSet中的同名表格
CommonReport.SetDataSource(dataSet.Tables[CommonReport.Database.Tables[0].Name]);
//绑定到报表浏览器
CrystalReportViewer1.ReportSource = CommonReport;
}
private void Page_Init(object sender, EventArgs e)
{
ConfigureCrystalReports();
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -