在VS 2005 和 VS 2008 中使用RDLC使用免费报表
来源:互联网 发布:淘宝代码转换器 编辑:程序博客网 时间:2024/05/16 23:35
我们知道在VS 2003中提供了Crystal Report,这为.NET报表程序开发带来的质的提高,但是它是收费的。 而现在VS 2005和VS 2008为我们提供了RDLC报表,它是完全免费的RDLC报表。RDLC的全称是Report Definition Language Client-Side Processing, 它所强调的是在客户端的处理能力。今天我就拿在项目中开发的报表出来说一说。
先来Show一下我的报表内容,我自己现在看看都感觉并不轻松,哈哈,不多说,Show起来。
通过上面的代码大家可看出报表相对比较复杂,即使这样复杂的报表RDLC也能完成,可想而知他并不逊色与向Crystal Report这样的报表,并且是免费的,更可贵的是他还拥有功能强大的公式功能及子报表功能。
下面的代码是如何设置提供数据及生成Report,至于如何设计Report我就不多说了,网络上已经有很多这方面的资料了。
参数提供代码
2 {
3 List<ReportDataSource> reportDataSource = new List<ReportDataSource>();
4 DataSet ds = new DataSet();
5 DataSet ds1 = new DataSet();
6 string templatePath = string.Empty;
7 string totalRecords = string.Empty;
8
9 //Genereate by spool
10 ds = SpoolManager.GetSpoolByIsoOid(Session["CurrentOperateProject"].ToString(), isoOid);
11 ds1 = ISODrawingManager.GetISODrawingByIdPipeClass(long.Parse(isoOid));
12 reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_T_Spool", ds.Tables[0]));
13 reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_ISO_DRAWING", ds1.Tables[0]));
14
15 //TemplateFiles
16 templatePath = "../ReportsTemplate/ReportQC13QC15.rdlc";
17 Project p = ProjectManager.GetProjectById(long.Parse(Session["CurrentOperateProject"].ToString()));
18 if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
19 totalRecords = "0";
20 else
21 totalRecords = ds.Tables[0].Rows.Count.ToString();
22 List<ReportParameter> parameterList = new List<ReportParameter>();
23 parameterList.Add(new ReportParameter("Report_ProjectName", p.ProjectName));
24 parameterList.Add(new ReportParameter("Report_ProjectOwner", p.Owner));
25 parameterList.Add(new ReportParameter("Report_ProjectNo", "2308"));
26 parameterList.Add(new ReportParameter("Report_SpoolCount", totalRecords));
27
28 ////Generate Report
29 GetReportMultipleDataSourceFile(reportDataSource, templatePath, parameterList, ConstantManager.REPORT_FORMAT_PDF_VALUE);
30 }
reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_T_Spool", ds.Tables[0]));
reportDataSource.Add(new ReportDataSource("QC13QC15DataSet_ISO_DRAWING", ds1.Tables[0]));
上面两条的作用是提供两个数据集给RDLC Report Template中,而在RDLC中我们经常将这种类型的数据集提供给Table对象。
生成Report代码
1 /// <summary>
2 /// Generate the report by below parameters
3 /// </summary>
4 /// <param name="reportDateSource">data source</param>
5 /// <param name="TemplatePath">template report file path</param>
6 /// <param name="parameterList">all parameters</param>
7 /// <param name="FileType">file type</param>
8 public void GetReportMultipleDataSourceFile(List<ReportDataSource> reportDateSource, string TemplatePath, List<ReportParameter> parameterList, string FileType)
9 {
10 string reportFormat = FileType;
11 string outputfile = "Report.";
12 ReportViewer rview = new ReportViewer();
13 rview.ProcessingMode = ProcessingMode.Local;
14 rview.LocalReport.ReportPath = Server.MapPath(TemplatePath);
15 rview.LocalReport.DataSources.Clear();
16 foreach (ReportDataSource re in reportDateSource)
17 {
18 rview.LocalReport.DataSources.Add(re);
19 }
20
21 if (parameterList.Count > 0)
22 rview.LocalReport.SetParameters(parameterList);
23 string mimeType, encoding, extension, deviceInfo;
24 string[] streamids;
25 Microsoft.Reporting.WebForms.Warning[] warnings;
26 deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>";
27
28 byte[] bytes = rview.LocalReport.Render(reportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
29
30 if (warnings != null && warnings.Length > 0)
31 {
32 LoggingManager log = LoggingManager.GetLoggingManager();
33 foreach (Warning w in warnings)
34 {
35 log.Info(w.Message);
36 }
37 }
38 HttpContext.Current.Response.Buffer = true;
39 HttpContext.Current.Response.Clear();
40 HttpContext.Current.Response.ContentType = mimeType;
41 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + outputfile + extension + ";");
42 HttpContext.Current.Response.BinaryWrite(bytes);
43 HttpContext.Current.Response.End();
44 }
我们定义了两个泛型参数List<ReportDataSource> reportDataSource = new List<ReportDataSource>()和List<ReportParameter> parameterList = new List<ReportParameter>(),因为在一个Report中可能需要从多个数据集或参数中提取数据,而这两个泛型参数正是充当了这样的角色。ReportDataSource类是用来提供数据集的,ReportParameter用来传递参数的。 可以在上面代码GetReportMultipleDataSourceFile中找到其用法。
在GetReportMultipleDataSourceFile方法的byte[] bytes = rview.LocalReport.Render(reportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings)语句中,值得注意的是参数reportFormat,通过这个参数我们可以生成不同文件类型的报表,在我的项目中,使用了两种生成格式--PDF及Excel两种,也就是说string FileType 这参数传递不同的类型格式,即可产生不同类型的报表。
以上并非难懂代码,本人只是将自己项目中用到的写出来,以便大家参考,如有不合理之处还请指正。
- 在VS 2005 和 VS 2008 中使用RDLC使用免费报表
- 在vs中后台打印rdlc报表
- 在VS.NET2005中使用免费报表(电脑报2006/09/11 第36期)
- 在VS.NET2005中使用免费报表(电脑报2006/09/11 第36期)
- 在VS.NET2005中使用免费报表(电脑报2006/09/11 第36期)
- 在 MVC 4 中使用RDLC 报表
- .C#中使用RDLC报表
- 水晶报表在VS中的使用
- vs 2010 中使用ReportViewer制作报表
- 在MVC中使用rdlc格式的报表
- 在VS 2005中使用TREEVIEW控件
- 在VS 2005中使用TREEVIEW控件
- 在VS中使用SVN
- 在vs中使用gtest
- 在VS中使用Protobuf
- RDLC报表中使用自定义函数
- C#中使用RDLC报表(1)
- VS2010中rdlc报表使用注意事项
- Java如何取源文件中文件名和行号
- 用 seagull 已有的module创建一个简单站点
- 动态库入口函数
- 最新kav激活授权文件(附使用方法)
- 在seagull框架下操作数据库
- 在VS 2005 和 VS 2008 中使用RDLC使用免费报表
- 抄袭来自http://blog.csdn.net/relative660/archive/2009/07/31/4398625.aspx WSDL逆向生成服务端
- dom javascript高级程序设计
- 谷歌大中华区总裁李开复离职
- 存储过程出现Size具有无效大小值
- Delphi2010新发现-类的构造和析构函数功能
- 心情—保持心态平衡(Balance)使人保持积极向上的情结和热情
- C FILE 操作
- [Start]开博了!