入门利用Grid++Report生成报表

一:利用sql语句,实际项目不用此方法填充数据引用的两个dll:AxInterop.gregn6Lib.dllInterop.gregn6Lib.dll
官网http://www.rubylong.cn/ 打开设计器设计;向导中可以设置数据库连接字符串连接SQLserver如下

Provider=SQLOLEDB.1;Password=密码;Persist Security Info=True;User ID=sa;Initial Catalog=数据库;Data Source=服务器名或者地址;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;
Workstation ID=WIN-JHN6DOOQRJN;Use Encryption for Data=False;Tag with column collation when possible=False

二:数据动态写入一般应该是数据库读取数据导出。或者不设置连接字符串将在程序中设置数据,下面以datatable插入到表中:注意字段集合名称最好不是中文,下面有说明

using gregn6Lib;
//定义Grid++Report报表主对象
private GridppReport Report = new GridppReport();
//载入报表模板数据
Report.LoadFromFile("模板地址");
//挂接报表事件的代码被多次执行,因此同一事件函数被多次挂接上;在事件一次触发过程中,这个事件函数会被多次调用。如 FetchRecord 事件就会多次填入报表数据,从而造成报表数据重复。
//所以现将数据事件删除然后重新挂接数据
Report.FetchRecord -= new _IGridppReportEvents_FetchRecordEventHandler(InsertTable);
 //连接报表事件先把数据填充到报表中
Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(InsertTable);
Report.PrintPreview(true);
//创建table插入
        private void InsertTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("StudentName",typeof(string));
            dt.Columns.Add("StudentNO", typeof(string));
            dt.Columns.Add("StudentAge", typeof(Int32));
            dt.Columns.Add("StudentClass", typeof(string));
            dt.Rows.Add(new object[] {"小米","123",18,"NO1"});
            dt.Rows.Add(new object[] { "魅族", "456", 19, "NO2" });
            dt.Rows.Add(new object[] { "苹果", "789", 20, "NO3" });
            IGRField StudentNameField = Report.FieldByName("StudentName");//此处根据字段名称获取需填充列
            IGRField StudentNOField = Report.FieldByName("StudentNO");
            IGRField StudentAgeField = Report.FieldByName("StudentAge");
            IGRField StudentClassField = Report.FieldByName("StudentClass");
            foreach (DataRow dr in dt.Rows)
            {
                //追加一行
                Report.DetailGrid.Recordset.Append();
                //给模板设置的每一列插入数据
                StudentNameField.AsString = dr["StudentName"].ToString();
                StudentNOField.AsString = dr["StudentNO"].ToString();
                StudentAgeField.AsInteger = int.Parse(dr["StudentAge"].ToString());
                StudentClassField.AsString = dr["StudentClass"].ToString();
                //插入
                Report.DetailGrid.Recordset.Post();
            }
        }

二:如果模板定义了连接字符串和sql,可以在程序中动态设置

//用代码设置报表主对象的数据库连接串
Report.ConnectionString = %动态连接串%;

//用代码设置明细记录集的数据库连接串
Report.DetailGrid.Recordset.ConnectionString = %动态连接串%;

//用代码设置报表主对象的查询SQL
Report.QuerySQL = %动态查询SQL%;

//用代码设置明细记录集的查询SQL
Report.DetailGrid.Recordset.QuerySQL = %动态查询SQL%;

//用代码设置图表记录集的的查询SQL
Report.ControlByName("Chart1").AsChart.Recordset.QuerySQL = %动态查询SQL%;


三:打印条码 Test.grf

using gregn6Lib;
namespace StudentTest
{
    public partial class Form1 : Form
    {
        private GridppReport Report = new GridppReport();
        public Form1()
        {
            InitializeComponent();
            Report.LoadFromFile(@"Test.grf");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //重复点击会产生重复数据
            Report.FetchRecord -= new _IGridppReportEvents_FetchRecordEventHandler(InsertTable);
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(InsertTable);
            Report.PrintPreview(true);
        }
        private void InsertTable()
        {
            string sql = "select cInvCode,cInvName,cInvStd,cBarCode from Inventory";
  DataTable dt = SqlHelper.ExecteQueryTable(SqlHelper.ConnectionString, CommandType.Text, sql, null)[0];
            IGRField cInvCode = Report.FieldByName("cInvCode");
            IGRField cInvName = Report.FieldByName("cInvName");
            IGRField cInvStd = Report.FieldByName("cInvStd");
            IGRField cBarCode = Report.FieldByName("cBarCode");
            foreach (DataRow dr in dt.Rows)
            {
                //追加一行
                Report.DetailGrid.Recordset.Append();
                //给模板设置的每一列插入数据
                cInvCode.AsString = dr["cInvCode"].ToString();
                cInvName.AsString = dr["cInvName"].ToString();
                cInvStd.AsString = dr["cInvStd"].ToString();
                cBarCode.AsString = dr["cBarCode"].ToString();0
                //插入
                Report.DetailGrid.Recordset.Post();
            }
        }
    }
}

检索COM类工厂中的CLSID....解决方法将目标平台设置为X86

posted @ 2026-09-06 09:39  清哥的码农生活  阅读(59)  评论(0)    收藏  举报