ReportViewer,RDLC报表开发之个性化样式
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ReportViewer,RDLC报表开发之个性化样式报表开发中,客户对样式提出了要求:
1.⼯具栏上显⽰每页条数
2.只导出Excel,不需要下拉菜单。
3.报表上显⽰的图表,分页时,每页都要显⽰,但导出后,图表是⼀个,且都在最下⾯。
另外的功能点:
4.每页显⽰标头
5.标题⾏和奇偶⾏样式
解决⽅案:
1.⼯具栏上显⽰每页条数
搜索⽆果后(RportViewer 没提供类似的功能吧.),暴⼒解决.好像⽹上也有类似的⽅案.
///<summary>
///给报表添加每页条数. Udi 2012年3⽉2⽇
///</summary>
///<param name="Report"></param>
///<param name="hdPageSize"></param>
public static void AddPageSize(this ReportViewer Report, HtmlInputHidden hdPageSize, Control Button)
{
var toolBar = FindToolBar(Report.Controls, "Microsoft.Reporting.WebForms.ToolbarControl");
//第1个是页码组,第2个是缩放组. 有依赖性.
var zoomGroup = toolBar.Controls[1];
var label = new LiteralControl();
label.ID = "lab_PageSize";
var items = new int[] { 5, 10, 20, 50 };
var options = new List<string>();
for (int i = 0; i < items.Length; i++)
{
options.Add(string.Format(@"<option value='{0}' {1}>{0}</option>", items[i], hdPageSize.Value == items[i].ToString() ? "selected" : "")); }
label.Text = string.Format(@"每页条数: <select onchange=""{0}"" style='width:40px'>{1}</select>",
string.Format(@"(function(item){{$('#{0}').val($(item).val()); $('#{1}').trigger('click');}})(this);",
hdPageSize.ClientID,
Button.ClientID),
string.Join("", options.ToArray()));
zoomGroup.Controls.AddAt(0, label);
}
private static Control FindToolBar(ControlCollection ReportControls, string FullTypeName)
{
foreach (Control item in ReportControls)
{
if (item.ToString() == FullTypeName) return item;
if (item.HasControls())
{
var subFind = FindToolBar(item.Controls, FullTypeName);
if (subFind != null) return subFind;
}
}
return null;
}
调⽤: (hdPageSize 是⼀个保存每页条数是Hidden, btnSearch 是查询按钮, 需要模拟点击)
this.ReportViewer1.AddPageSize(this.hdPageSize, this.btnSearch);
2. 只导出Excel,不需要下拉菜单。
思路和上⾯差不多,索性写的⽅法再通⽤⼀点。
///<summary>
///给 ReportViewer 添加控件.
///</summary>
///<param name="Report"></param>
///<param name="GroupIndex">组索引</param>
///<param name="ControlIndex">组内控件索引</param>
///<param name="Control"></param>
public static void AddToolControl(this ReportViewer Report, int GroupIndex, int ControlIndex, Control Control)
{
var toolBar = FindToolBar(Report.Controls, "Microsoft.Reporting.WebForms.ToolbarControl");
var zoomGroup = toolBar.Controls[GroupIndex];
zoomGroup.Controls.AddAt(ControlIndex, Control);
}
调⽤:(第⼀个索引,表⽰组索引,第⼆个索引表⽰组内控件索引)
this.ReportViewer1.AddToolControl(5, 0, new LiteralControl() { Text = string.Format(@"<a class='expBtn'
onclick=""document.getElementById('{0}').click();"">导出</>", this.btn_Excel.ClientID) });
3. 报表上显⽰的图表,分页时,每页都要显⽰,但导出后,图表是⼀个,且都在最下⾯。
之前是⼀个页⾯上绑定了⼀个 ReportViewer , ⼀个 RDLC . 两个数据源(⼀个⽤于分页, ⼀个⽤于出图. 其实没必要啊.反正也要取所有数据出图表,应该⽤⼀个全量数据源即可.这样做,多多少少受了分页⽅案的影响.),效果是只在最后⼀页显⽰图表.
把 RDLC 拆成三部分.
第⼀部分: 数据+图表. (导出⽤.)
第⼆部分: 数据
第三部分: 图表
页⾯上添加两个 ReportViewer
第⼀个: 显⽰数据
第⼆个: 显⽰图表 (隐藏⼯具栏)
看起来像⼀个.
显⽰时, Report_Data 绑定 RDLC_Data , Report_Chart 绑定 RDLC_Chart .
导出时, 使⽤ Report_Data 绑定 RDLC_Data_Chart .
即可.
4. 每页显⽰标题的设置⽅法:
RDLC ⽂件视图中: 选列组, 点⾼级模式 , ⾏组出现静态(⼆级样式) . 在根静态上 f4, 设置:
KeepWithGroup: After
RepeateOnNewPage:True
属性表⾥选 : Tablix,设置:
RepeatColumnHeaders : True
RepeatRowHeaders : True
5.标题样式好说, 奇偶⾏样式设置
先中⾏,在 BackGroundColor 属性⾥设置表达式:
=IIF( ( RowNumber( Nothing ) +Fields!SysPageIndex.Value ) Mod 2 =1 , "#eeeeee" , "#fff")注: RowNumber( Nothing ) 表⽰⾏号。
6.导出事件
$find('ReportViewer1')._getInternalViewer().ExportReport = function (format) {
if (this.ExportUrlBase == null)
return false;
var format = encodeURIComponent(format).toLowerCase();
switch (format) {
case"excel":
document.getElementById("<%= btn_Excel.ClientID%>").click();
break;
case"pdf":
document.getElementById("<%= btn_PDF.ClientID%>").click();
break;
case"word":
document.getElementById("<%= btn_Word.ClientID%>").click();
break;
}
return true;
}。