最近一个项目中,涉及到很多报表的开发,以前对水晶报表搞得比较熟,但它的庞大与其它诸多功能限制,特别是发布与部置时面临的困难,实在让人无法忍受(应该很少有人买得起正版吧),决定试一下MS Reporting Services,当然在这里只使用Local Report,对微软的Reporting Services进行了一番了解,蜡人张(http://www.cnblogs.com/waxdoll/category/49402.html)老兄这里有很多很好的学习材料,我也是从蜡人兄那里对自定义报表的研究得出以下部分修正的Demo.
对于微软提供的ReportView控件,大家都知道,不能在其中对报表的边距等进行自定义,其工具条也是根本上无法自定义控制的,每次用户打印时,都必须对页面,边距等进行调整,对用户来说,这可能是一件无法忍受的事情,这或者是为满足Reporting Services报表而这样设计的.
对蜡人张老兄提供的例子做了一些改进,主要修正以下几点:
1,修正无法使用网络打印机进行打印的问题;
2,报表使用钳入式资源加载方式;
3,提取成一用户控件ReportView,更方便于开发人员将其整合到自己的项目中
Demo文件结构:
自定义报表控件ReportView源码分如下:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.IO;
9
using Microsoft.Reporting.WinForms;
10
namespace LocalReportDemo.Report
11
2
3
4
5
6
7
8
9
10
11
报表宿主窗口加载报表与数据源的方法:
1
DataSet ds = new DataSet();
2
ds.ReadXml(Application.StartupPath + @"\DataSource.xml");
3
this.reportViewer1.MainDataSet = ds;//报表主数据源
4
this.reportViewer1.MainDataSourceName = "ReportDemoDataSet_ReportDemoTable";//数据源名称
5
this.reportViewer1.MReportEmbeddedResource = "LocalReportDemo.ReportDemo.rdlc";//钳入资源的报表路径
6
this.reportViewer1.ReportName = "ReportDemo";//报表名称
7
this.reportViewer1.BindReport();//加载挷定报表
2
3
4
5
6
7
数据源名称如下图所示:
其中钳入资源的报表路径中,LocalReportDemo为报表钳入的程序集名称
报表格式配置文件ReportSettings.xml:
1
<?xml version="1.0" standalone="yes"?>
2
<ReportSettings>
3
<ReportDemo>
4
<ReportName>ReportDemo</ReportName>
5
<PrinterName>Microsoft Office Document Image Writer</PrinterName>
6
<PaperName>A4</PaperName>
7
<PageWidth>21.01</PageWidth>
8
<PageHeight>29.69</PageHeight>
9
<MarginTop>0.5</MarginTop>
10
<MarginBottom>0.2</MarginBottom>
11
<MarginLeft>2.3</MarginLeft>
12
<MarginRight>0.2</MarginRight>
13
<Orientation>Z</Orientation>
14
</ReportDemo>
15
</ReportSettings>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
此Demo运行效果:
打印预览:
有任何问题欢迎在此提出,大家共同讨论解决
此Demo完整代码下载:
/Files/mshwu/LocalReportDemo.rar