刚开始接触锐浪报表,因为在项目中有关于打印的部分,所以需要用到报表,一开始去看关于打印的代码的时候,并不知道报表是个什么东西,只知道它是一个表格,在看了关于打印的部分的代码的时候,大概了解了是怎么回事。
打印大概分为两部分:
第一部分:得到DataTable
1、先根据CheckBox的checked属性来判断用户勾选了哪些项目
2、根据CheckBox的checked来生成相应的需要查询字段
3、用这些字段查询以后得到一个DataSet数据集
4、将dataset转成DataTable
第二部分:将DataTable的数据填充到报表中
5、在执行打印的时候,需要连接报表事件,报表事件有两个,一个是获取报表列,一个是填充数据。
6、在执行打印时,需要载入自定义报表,以及判断显示哪些列。
//锐浪报表的设计:
锐浪报表有点类似于DataGridView,我们主要使用的是明细网格的部分,明细网格中添加我们需要的内容行跟标题行以及数据集,将数据放入报表以后就能显示出来了。
后面是详细的设计和代码:
Form设计如下图所示:
在页面代码中添加一个全局变量dt以及实例化打印报表类:
public partial class print : Form
{
///
/// 打印报表类
///
PrintReport report = new PrintReport();
DataTable dt;
以下是查询的代码:
private void btnQuery_Click(object sender, EventArgs e)
{
//查询
string fileId = "";//要查询的字段
string where = "";
if(comboBox1.Text == "")
{
fileId +=",leixing";
where = "where 1=1";
dataGridView1.Columns["leixingColumn"].Visible = true;
}
if(comboBox1.Text != "")
{
fileId += ",leixing";
where = "where leixing='"+ comboBox1.Text +"'";
dataGridView1.Columns["leixingColumn"].Visible = true;
}
if(ckbKeys.Checked)
{
fileId += ",keys";
dataGridView1.Columns["keysColumn"].Visible = true;
}
if(ckbDescribe.Checked)
{
fileId += ",describe";
dataGridView1.Columns["describeColumn"].Visible = true;
}
if(ckbQQ.Checked)
{
fileId += ",qq";
dataGridView1.Columns["qqColumn"].Visible = true;
}
if(ckbVX.Checked)
{
fileId += ",vx";
dataGridView1.Columns["vxColumn"].Visible = true;
}
if(ckbZujin.Checked)
{
fileId += ",zujin";
dataGridView1.Columns["zujinColumn"].Visible = true;
}
string sql = "select " + fileId.Substring(1) + ",name,userid from pass "+ where +"";
//根据sql语句和你要操作的数据库名得到datatable
sqlOpr sqlopr = new sqlOpr();
dt = sqlopr.GetTableBySql(sql,"fast");
BindingSource bs = new BindingSource();
bs.DataSource = dt;
this.dataGridView1.DataSource = bs;
}
sqlopr.GetTableBySql()方法的详细代码如下:
//根据sql语句得到一个datatable
public DataTable GetTableBySql(string sqlstring,string sqlname)
{
connstr = "server=" + server + ";uid=" + user + ";pwd=" + pwd + ";database=" + sqlname + "";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand comm = new SqlCommand(sqlstring, conn);
SqlDataReader dr = comm.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
return dt;
}
以下是打印的代码:
private void btnPrint_Click(object sender, EventArgs e)
{
// 打印
//判断datatable是不是空的
if (dt != null)
{
report.dt = dt;
report.ckbKeys = ckbKeys;
report.ckbDescribe = ckbDescribe;
report.ckbQQ = ckbQQ;
report.ckbVX = ckbVX;
report.ckbZujin = ckbZujin;
//执行打印方法
report.PrintReportFile();
}
else
{
MessageBox.Show("请先查询出要打印的数据");
return;
}
}
完整的打印报表类代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
//引用
using gregn6Lib;
namespace EMS
{
///
/// 打印类
///
class PrintReport
{
private GridppReport report = new GridppReport();
//定义报表主对象
private IGRField IGRkeysField;
private IGRField IGRnameField;
private IGRField IGRdescribeField;
private IGRField IGRqqField;
private IGRField IGRvxField;
private IGRField IGRuseridField;
private IGRField IGRleixingField;
private IGRField IGRzujinField;
//表
public DataTable dt;
//控制显示列
public CheckBox ckbZujin;
public CheckBox ckbKeys;
public CheckBox ckbDescribe;
public CheckBox ckbQQ;
public CheckBox ckbVX;
public void initReport()
{
//初始化报表
//连接报表事件
report.Initialize += new _IGridppReportEvents_InitializeEventHandler(getReportField);
report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(setData);
}
public void getReportField()
{
//获取报表列
IGRkeysField = report.FieldByName("keys");
IGRnameField = report.FieldByName("name");
IGRdescribeField = report.FieldByName("describe");
IGRqqField = report.FieldByName("qq");
IGRvxField = report.FieldByName("vx");
IGRuseridField = report.FieldByName("userid");
IGRleixingField = report.FieldByName("leixing");
IGRzujinField = report.FieldByName("zujin");
}
public void setData()
{
//填充数据
DataRow dr;
for (int i = 0; i < dt.Rows.Count - 1; i++)
{
dr = dt.Rows[i];
report.DetailGrid.Recordset.Append();
if (ckbKeys.Checked)
{
IGRkeysField.AsString = dr["keys"].ToString();
}
if (ckbDescribe.Checked)
{
IGRdescribeField.AsString = dr["describe"].ToString();
}
if (ckbQQ.Checked)
{
IGRqqField.AsString = dr["qq"].ToString();
}
if (ckbVX.Checked)
{
IGRvxField.AsString = dr["vx"].ToString();
}
if (ckbZujin.Checked)
{
IGRzujinField.AsString = dr["zujin"].ToString();
}
//待打印的报表的记录集中是否存在列
//IGRleixingField.AsString = dr["leixing"].ToString();
//IGRuseridField.AsString = dr["userid"].ToString();
//IGRnameField.AsString = dr["name"].ToString();
if (dt.Columns.Contains("leixing"))//rows
{
IGRleixingField.AsString = dr["leixing"].ToString();
}
if (dt.Columns.Contains("userid"))
{
IGRuseridField.AsString = dr["userid"].ToString();
}
if (dt.Columns.Contains("name"))
{
IGRnameField.AsString = dr["name"].ToString();
}
report.DetailGrid.Recordset.Post();
}
}
public void PrintReportFile()
{
//执行打印
//载入报表
report.LoadFromFile("E:\\个人文件\\GET\\C#\\EMS\\EMS\\GRF\\demo.grf");
//显示哪些列
report.ColumnByName("keys").Visible = ckbKeys.Checked;
report.ColumnByName("qq").Visible = ckbQQ.Checked;
report.ColumnByName("vx").Visible = ckbVX.Checked;
report.ColumnByName("describe").Visible = ckbDescribe.Checked;
report.ColumnByName("zujin").Visible = ckbZujin.Checked;
//report.ColumnByName("leixing").Visible = true;
//report.ColumnByName("userid").Visible = true;
//report.ColumnByName("name").Visible = true;
///打印预览
report.PrintPreview(true);
}
}
}