码迷,mamicode.com
首页 > Windows程序 > 详细

C#制作RDLC报表

时间:2014-12-01 12:42:54      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   io   ar   color   os   使用   

报表以前我只做过水晶报表,但是最近发现家里的VS上面居然没有水晶报表,发现水晶报表现在貌似已经不能完全免费的使用了,为了保险起见,就用了VS自带的RDLC报表,用完感觉其实也是够用的嘛~

建立一RDLC报表的过程主要是:

1、新建一个windows窗口,拖一个Reportview控件在上面,用来显示报表

2、新建一个类,构成数据源:

需要哪些字段,就在类中添加哪些,比如简单的如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace land_useowner_db
{
    class FarmerReportClass    //农户数据源
    {
        public string farmerBM { get;set; }
        public string farmerMC { get; set; }
        public string farmerLXDH { get; set; }
        public string farmerZJLX { get; set; }
        public string farmerZJHM { get; set; }
        public string farmerCBFDZ { get; set; }
        public string farmerYZBM { get; set; }
        public string farmerCBFCYSL { get; set; } 
    }
}

数据源字段设置好后就要添加到数据库中:》bubuko.com,布布扣

bubuko.com,布布扣

选择新建好的数据源类:bubuko.com,布布扣  

数据源添加好了,接下来就是给数据源赋予值了

3、新建一个报表

bubuko.com,布布扣

查看代码:

bubuko.com,布布扣
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 using System.Data.OleDb;
 11 using Microsoft.Reporting.WinForms;
 12 
 13 namespace land_useowner_db.统计报表
 14 {
 15     public partial class FarmerReport : Form
 16     {
 17         string strCBFBM;
 18         string strCBFMC;
 19         string filepath;
 20         public FarmerReport(string CBFBM,string CBFMC,string path)
 21         {
 22             this.strCBFBM = CBFBM;
 23             this.strCBFMC = CBFMC;
 24             this.filepath = path;
 25             InitializeComponent();
 26         }
 27 
 28         private void FarmerReport_Load(object sender, EventArgs e)
 29         {
 30             #region test 
 31             //this.reportViewer1.LocalReport.ReportEmbeddedResource = "land_useowner_db.统计报表.Report1.rdlc";
 32             //Microsoft.Reporting.WinForms.ReportParameter rp = new Microsoft.Reporting.WinForms.ReportParameter("cbfbm", strCBFBM);
 33             //this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { rp });
 34             //this.reportViewer1.RefreshReport();
 35 
 36       
 37             //为报表浏览器指定报表文件
 38 
 39             //this.reportViewer1.LocalReport.ReportEmbeddedResource = "report.Report1.rdlc";
 40 
 41             //指定数据集,数据集名称后为表,不是DataSet类型的数据集
 42             #endregion 
 43 

 44             this.reportViewer1.LocalReport.DataSources.Clear();
 45             this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("land_useowner_db_FarmerReportClass", GetList()));
 46 
 47             //显示报表
 48             this.reportViewer1.RefreshReport();            
 49         }
 50 
 51 
 52         /// <summary>
 53         /// 获取打印的数据源(CBF表中)
 54         /// </summary>
 55         /// <returns></returns>
 56         private List<FarmerReportClass> GetList()
 57         {
 58 
 59             //取得数据集
 60             string sql = "select * from CBF where CBFLX=‘1‘ ";
 61             sql += "and CBFBM= ‘" + strCBFBM + "";
 62             // 连接数据库 
 63             OleDbConnection connct = new OleDbConnection();
 64             string oleDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath;
 65             connct.ConnectionString = oleDB;
 66             //创建命令
 67             OleDbCommand command = new OleDbCommand(sql, connct);
 68             //执行命令          
 69             OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
 70             DataTable table = new DataTable();         
 71             try
 72             {
 73                 connct.Open();
 74                 dataAdapter.Fill(table);
 75 
 76                 List<FarmerReportClass> list = new List<FarmerReportClass>();
 77                 FarmerReportClass stu = null;
 78                 foreach (DataRow row in table.Rows)
 79                 {
 80                     stu = new FarmerReportClass();
 81                     if (row.Table.Columns.Contains("CBFBM") && row["CBFBM"] != null && row["CBFBM"].ToString() != "")
 82                     {
 83                         stu.farmerBM = row["CBFBM"].ToString();
 84                     }
 85                     if (row.Table.Columns.Contains("CBFMC") && row["CBFMC"] != null && row["CBFMC"].ToString() != "")
 86                     {
 87                         stu.farmerMC = row["CBFMC"].ToString();
 88                     }
 89                     if (row.Table.Columns.Contains("LXDH") && row["LXDH"] != null && row["LXDH"].ToString() != "")
 90                     {
 91                         stu.farmerLXDH = row["LXDH"].ToString(); ;
 92                     }
 93                     if (row.Table.Columns.Contains("CBFZJLX") && row["CBFZJLX"] != null && row["CBFZJLX"].ToString() != "")
 94                     {
 95                         string zjlx = row["CBFZJLX"].ToString();
 96                         switch (zjlx)
 97                         {
 98                             case "1": zjlx = "居民身份证"; break;
 99                             case "2": zjlx = "军官证"; break;
100                             case "3": zjlx = "行政、企事业单位机构代码证或法人代码证"; break;
101                             case "4": zjlx = "户口簿"; break;
102                             case "5": zjlx = "护照"; break;
103                             case "9": zjlx = "其他证件"; break;
104                         }
105                         stu.farmerZJLX = zjlx;
106                     }
107                     if (row.Table.Columns.Contains("CBFZJHM") && row["CBFZJHM"] != null && row["CBFZJHM"].ToString() != "")
108                     {
109                         stu.farmerZJHM = row["CBFZJHM"].ToString(); ;
110                     }
111                     if (row.Table.Columns.Contains("CBFDZ") && row["CBFDZ"] != null && row["CBFDZ"].ToString() != "")
112                     {
113                         stu.farmerCBFDZ = row["CBFDZ"].ToString(); ;
114                     }
115                     if (row.Table.Columns.Contains("YZBM") && row["YZBM"] != null && row["YZBM"].ToString() != "")
116                     {
117                         stu.farmerYZBM= row["YZBM"].ToString(); ;
118                     }
119                     if (row.Table.Columns.Contains("CBFCYSL") && row["CBFCYSL"] != null && row["CBFCYSL"].ToString() != "")
120                     {
121                         stu.farmerCBFCYSL = row["CBFCYSL"].ToString(); ;
122                     }
123                     list.Add(stu);
124                 }
125                 return list;
126             }
127             finally
128             {
129                 connct.Close();
130                 command.Dispose();
131                 connct.Dispose();
132             }
133    
134         }
View Code

 

4、在新建好的报表中就可以通过拖动相应的控件等来设计报表,简单的说就是画报表,安装需求画好相应的报表内容,比如:

bubuko.com,布布扣

C#制作RDLC报表

标签:winform   style   blog   http   io   ar   color   os   使用   

原文地址:http://www.cnblogs.com/smilepeter/p/4134677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!