C#制作RDLC报表

时间:2021-11-18 22:40:57

报表以前我只做过水晶报表,但是最近发现家里的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; }
}
}

数据源字段设置好后就要添加到数据库中:》C#制作RDLC报表

C#制作RDLC报表

选择新建好的数据源类:C#制作RDLC报表  

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

3、新建一个报表

C#制作RDLC报表

查看代码:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Data.OleDb;
using Microsoft.Reporting.WinForms; namespace land_useowner_db.统计报表
{
public partial class FarmerReport : Form
{
string strCBFBM;
string strCBFMC;
string filepath;
public FarmerReport(string CBFBM,string CBFMC,string path)
{
this.strCBFBM = CBFBM;
this.strCBFMC = CBFMC;
this.filepath = path;
InitializeComponent();
} private void FarmerReport_Load(object sender, EventArgs e)
{
#region test
//this.reportViewer1.LocalReport.ReportEmbeddedResource = "land_useowner_db.统计报表.Report1.rdlc";
//Microsoft.Reporting.WinForms.ReportParameter rp = new Microsoft.Reporting.WinForms.ReportParameter("cbfbm", strCBFBM);
//this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter[] { rp });
//this.reportViewer1.RefreshReport(); //为报表浏览器指定报表文件 //this.reportViewer1.LocalReport.ReportEmbeddedResource = "report.Report1.rdlc"; //指定数据集,数据集名称后为表,不是DataSet类型的数据集
#endregion this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("land_useowner_db_FarmerReportClass", GetList())); //显示报表
this.reportViewer1.RefreshReport();
} /// <summary>
/// 获取打印的数据源(CBF表中)
/// </summary>
/// <returns></returns>
private List<FarmerReportClass> GetList()
{ //取得数据集
string sql = "select * from CBF where CBFLX='1' ";
sql += "and CBFBM= '" + strCBFBM + "'";
// 连接数据库
OleDbConnection connct = new OleDbConnection();
string oleDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath;
connct.ConnectionString = oleDB;
//创建命令
OleDbCommand command = new OleDbCommand(sql, connct);
//执行命令
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
DataTable table = new DataTable();
try
{
connct.Open();
dataAdapter.Fill(table); List<FarmerReportClass> list = new List<FarmerReportClass>();
FarmerReportClass stu = null;
foreach (DataRow row in table.Rows)
{
stu = new FarmerReportClass();
if (row.Table.Columns.Contains("CBFBM") && row["CBFBM"] != null && row["CBFBM"].ToString() != "")
{
stu.farmerBM = row["CBFBM"].ToString();
}
if (row.Table.Columns.Contains("CBFMC") && row["CBFMC"] != null && row["CBFMC"].ToString() != "")
{
stu.farmerMC = row["CBFMC"].ToString();
}
if (row.Table.Columns.Contains("LXDH") && row["LXDH"] != null && row["LXDH"].ToString() != "")
{
stu.farmerLXDH = row["LXDH"].ToString(); ;
}
if (row.Table.Columns.Contains("CBFZJLX") && row["CBFZJLX"] != null && row["CBFZJLX"].ToString() != "")
{
string zjlx = row["CBFZJLX"].ToString();
switch (zjlx)
{
case "": zjlx = "居民身份证"; break;
case "": zjlx = "军官证"; break;
case "": zjlx = "行政、企事业单位机构代码证或法人代码证"; break;
case "": zjlx = "户口簿"; break;
case "": zjlx = "护照"; break;
case "": zjlx = "其他证件"; break;
}
stu.farmerZJLX = zjlx;
}
if (row.Table.Columns.Contains("CBFZJHM") && row["CBFZJHM"] != null && row["CBFZJHM"].ToString() != "")
{
stu.farmerZJHM = row["CBFZJHM"].ToString(); ;
}
if (row.Table.Columns.Contains("CBFDZ") && row["CBFDZ"] != null && row["CBFDZ"].ToString() != "")
{
stu.farmerCBFDZ = row["CBFDZ"].ToString(); ;
}
if (row.Table.Columns.Contains("YZBM") && row["YZBM"] != null && row["YZBM"].ToString() != "")
{
stu.farmerYZBM= row["YZBM"].ToString(); ;
}
if (row.Table.Columns.Contains("CBFCYSL") && row["CBFCYSL"] != null && row["CBFCYSL"].ToString() != "")
{
stu.farmerCBFCYSL = row["CBFCYSL"].ToString(); ;
}
list.Add(stu);
}
return list;
}
finally
{
connct.Close();
command.Dispose();
connct.Dispose();
} }

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

C#制作RDLC报表