打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[原创] RDLC 报表系列(一) 创建一个报表 - 大熊的空间 - 博客园

[原创] RDLC 报表系列(一) 创建一个报表

    本文只代表作者在一定阶段的认识与理解。

 .写作前提

前一段时间写了一篇关于RDLC开发的示例文章,[原创] VS 2005 VS 2008 中使用RDLC使用免费报表,得到了群里很多兄弟的支持,这篇文章从大体上讲述了怎样用RDLC做报表开发,没有给出具体的开发步骤,因此我决定来写一个系列关于RDLC报表开发的文章,希望对你有所帮助。

这个系列文章主要从实例的方式来说明怎么用Visual Studio 20082005也一样提供这个功能)做RDLC报表的开发,最后再对此系列做总结,讲述一些概念的东西。

            作为开篇,我不想过多的讲于概念,主要来说明怎样来实现一个简单的RDLC报表。

.本文内容  

1.创建一个简单RDLC项目

2.总结
3.代码下载(下载

三.创建一个简单RDLC项目

1. 打开Visual Studio 2008,新建一个ASP.NET Web Application项目,如下图所示。




2. 在项目中新建两个文件夹,分别叫DataEntityReprotTemplate,如下图, 至于这两个文件夹主要用来存放什么,在下面我们会讲到,这里先不说。

 




3. 此项目需要引用Microsoft.Reporting.WinFormsSystem.Windows.Forms,所以我们需要右击上图中的References,然后在.NET Tab中找到我们所需要引用的Reference,点击Ok,添加到我们的引用中去。

 



4. 右击我们新建的DataEntity文件夹,选择Addà New Item…,选择左边的Categories中的Data,然后选择右边的DataSet,输入你的DataSet 名字,然后点击Add到这个文件夹下,至此我们知道DataEntity文件夹用来存放数据集。

 


5. 点击Visual Studio IDE左边的Server Explorer(如果左边没有,可以在View菜单中找到),然后选择一个Database中的表,选择拖到DataSet中(如果还没有IDE中还没有Database影射,那么可以选择Toolsà Connect To Database…来连接到你需要的Database),当然这个DataSet中的表也可以自己定义(在编辑区点击右键,选择新增表,然后在表中增加你需要的字段)。

 





6. 右击我们新建的ReprotTemplate文件夹,选择Addà New Item…,选择左边的Categories中的Reporting,然后选择右边的Report,输入你需要的名字,点击Add,就样我们就把一个Report的模板加入到ReprotTemplate文件夹中了,Report模板的编辑区如下面第二张图所示。

 

 






7. 下面我们要做的事情就是编辑这个报表模板,首先我们选择这个报表所需要的数据集,点击Report菜单下面的Data Source…,在弹出的Report Data Source中,选择我们添加在DataEntity中那个DataSet的表,如下图所示。这里也就说明了,DataEntity中存放的是RDLC模板中所需要的数据集,为RDLC提供数据服务(除了DataSet以外,还有很多数据集,我们以后的内容中会讲解这此)。

 

 





8. 然后我们在Visual Studio IDE的左边的ToolBox中拖一个Table到我们的报表编辑区中,这个Table默认有三行三列,第一行是标题行,第二行是数据绑定行,第三行是表的页脚行(如果你不想显示其中的一部分你可以选中一行,然后右击这一行的最左边,取消或者选中即可)。如下图所示,我们在标题行对应的字段中输入标题名,你也可以右击到某一列,选择新增一列在选择列的左边或者右边。

            接下来要做的事情就是为这个表选择数据集,选择表,右击表,选择Properties,在弹出的Table Properties General TabDataSet Name中选择我们刚到加入的数据集,你需要记住这个名字,因为有的时候你可能会用到。

 

 




9. 绑定数据到数据行,选择数据行中的第一列,右击选择Expression…,然后在Expression窗口中,选择Category中的,DataSets,然后Item中自动的会列出你已经增加的Data Source,选择一个Data Source,他会为你列出所有的字段,选中你要的字段双击即可到上面的编辑框中(默认的没有直接绑定到每一行的数据),你也可以在编辑框中自己输入如Fields!Category.Value来选择数据,其中Category是字段名,当你输入Fields!后会自动提示你有哪此字段。选择后点击OK,其它的字段也以同样的方法设置即可。

 

 




10. 上面我们已经完成了新增数据集,设置报表模板,为模板中的控件绑定数据集,那下面我们就来看一下如何提供数据给这个数据集,以及如何实现报表。

            首先,我们在项目的根目录下新建一个类叫CustomPageBase.cs,他主要用来实现生成报表,代码如下:

 

 1 #region Copyright(C) 2009 Xiong Wei All rights reserved.
 2 
 3  // ==============================================================================
 4  // Copyright(C) 2009  Xiong Wei
 5  //
 6  // SYSTEM NAME            : 
 7  // COMPONENT ID            : RCLC.CustomPageBase
 8  // COMPONENT DESC       : 
 9  //
10  // CREATED DATE/BY        : 2009 / Xiong Wei
11  //
12  // REVISION HISTORY     :
13  // DATE/BY  ISSUE#/SR#/CS/PM#/OTHERS    DESCRIPTION OF CHANGE
14  // 
15  // 
16  // ==============================================================================
17  
18  #endregion
19  using System;
20  using System.Data;
21  using System.Configuration;
22  using System.Linq;
23  using System.Web;
24  using System.Web.Security;
25  using System.Web.UI;
26  using System.Collections.Generic;
27  using Microsoft.Reporting.WinForms;
28 
29  namespace RCLC
30 {
31     public class CustomPageBase : Page
32     {
33         public void GetReportMultipleDataSourceFile(List<ReportDataSource> reportDateSource, string TemplatePath, List<ReportParameter> parameterList, string FileType)
34         {
35             string reportFormat = FileType;
36             string outputfile = "Report.";  //报表名称
37              ReportViewer rview = new ReportViewer();
38             rview.ProcessingMode = ProcessingMode.Local;
39             rview.LocalReport.ReportPath = Server.MapPath(TemplatePath);
40             rview.LocalReport.DataSources.Clear();
41             foreach (ReportDataSource re in reportDateSource)
42             {
43                 rview.LocalReport.DataSources.Add(re);
44             }
45 
46             if (parameterList.Count > 0)
47                 rview.LocalReport.SetParameters(parameterList);
48             string mimeType, encoding, extension, deviceInfo;
49             string[] streamids;
50             Warning[] warnings;
51             deviceInfo = "<DeviceInfo>" + "<SimplePageHeaders>True</SimplePageHeaders>" + "</DeviceInfo>";
52 
53             byte[] bytes = rview.LocalReport.Render(reportFormat, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
54 
55             //这里可以输入产生报表的时候有哪些警告信息
56             //if (warnings != null && warnings.Length > 0)
57             //{
58             //    LoggingManager log = LoggingManager.GetLoggingManager();
59             //    foreach (Warning w in warnings)
60             //    {
61             //        log.Info(w.Message);
62             //    }
63             //}
64              HttpContext.Current.Response.Buffer = true;
65             HttpContext.Current.Response.Clear();
66             HttpContext.Current.Response.ContentType = mimeType;
67             HttpContext.Current.Response.AddHeader("Content-Disposition""attachment; filename=" + outputfile + extension + ";");
68             HttpContext.Current.Response.BinaryWrite(bytes);
69             HttpContext.Current.Response.End();
70         }
71     }
72 }
73  

       然后让Default.aspx.cs继承CustomPageBase,然后在Default页面中新增一个Button,在Button的事件中提供产生Report所需要的参数,如下:

 

 1 using System;
 2  using System.Collections;
 3  using System.Configuration;
 4  using System.Data;
 5  using System.Linq;
 6  using System.Web;
 7  using System.Web.Security;
 8  using System.Web.UI;
 9  using System.Web.UI.HtmlControls;
10  using System.Web.UI.WebControls;
11  using System.Web.UI.WebControls.WebParts;
12  using System.Data.SqlClient;
13  using System.Collections.Generic;
14  using System.Xml.Linq;
15  using Microsoft.Reporting.WinForms;
16  using RCLC.DataEntity;
17 
18  namespace RCLC
19 {
20     public partial class _Default : CustomPageBase
21     {
22         protected void Page_Load(object sender, EventArgs e)
23         {
24 
25         }
26 
27         protected void ButtonReportGenerate_Click(object sender, EventArgs e)
28         {
29             List<ReportDataSource> reportDataSource = new List<ReportDataSource>();
30             RportDataSet ds = new RportDataSet();
31             string templatePath = string.Empty;
32             string totalRecords = string.Empty;
33 
34             //获得数据
35              SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoggingConnectionString"].ConnectionString);
36             SqlCommand command = conn.CreateCommand();
37             command.CommandType = CommandType.Text;
38             command.CommandText = "SELECT * FROM T_BC_LOGS";
39             SqlDataAdapter da = new SqlDataAdapter(command);
40             da.Fill(ds.T_BC_LOGS);
41 
42             //指定报表模板
43              templatePath = "ReportTemplate/LogReport.rdlc";
44 
45             //把获取的数据集合提供给在报表中名为RportDataSet_T_BC_LOGS数据集
46              reportDataSource.Add(new ReportDataSource("RportDataSet_T_BC_LOGS", ds.T_BC_LOGS));
47             List<ReportParameter> parameterList = new List<ReportParameter>();
48             ////Generate Report, 报表可以生成PDF,EXCEL及以其它形式,根据需求去设置
49              GetReportMultipleDataSourceFile(reportDataSource, templatePath, parameterList, "pdf");
50         }
51     }
52 }
53  

            最后,选择这个页面,点击button就呆以产生你所需要的页面了,如下图所示。

 


四.总结

            通过这个项目,我们可以了解到如下内容:

  1. 如何创建一个DataSet
  2. 创建和编辑RDLC报表模板并绑定到数据集上
  3. 通过代码为RDLC提供参数
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
VS2010 rdlc报表 一个报表如何显示多个数据源
VS2005中ReportViewer 本地模式下报表呈现 入门示例(转)
RDLC 子报表实例
RDLC报表 中略去XSD文件的支持
在VS2010Winform项目中使用RDLC报表定义文件、ReportViewer控件生...
使用RDLC做报表
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服