在C#(winform)中调用AcReport报表引擎
作者:admin
更新时间:2022-08-18
点击数:
来源:https://www.cnblogs.com/CelonY/p/14369764.html
1、引用AcReport.dll组件
2、注册组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /// <summary> /// 注册组件 /// </summary> AcReport.AcRptEngine mac; private void SetAcRegisterInfo() { int ErrCode = 0; string ErrMsg = "" ; mac.SetRegisterInfo( "280853595D4033132E36CC85879681948B9690A4978D8A85CA878B89C49595DED5D2D1D1D7D" + "3D7DCD1DDCDD6D78680D8AD0CFAADC1DDF8F566934E1BAD6B8B296DB4BC968283E9F8FE23728EF0F71F9417C40DB6D30C729ECD01D774746D80E3EE321C6D" , "天方工作室(acreport@sina.com qq:1655373859)" , "23bd" , "" , "" , "" , "" , ref ErrCode, ref ErrMsg); if (ErrCode != 0) MessageBox.Show(ErrMsg); } |
3、窗体加载时候初始化:注册AcReport组件
1 2 3 | ReportPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath); mac = new AcReport.AcRptEngine(); SetAcRegisterInfo(); |
实例化一个AcRptEngine对象,AcReport中只有一个实例化对象AcRptEngine。
4、定义dapper访问数据库
1 2 3 4 5 6 7 | static string sqlconnectionString = ConfigurationManager.ConnectionStrings[ "constr" ].ToString(); //获取App.config中的连接字符串 public static SqlConnection SqlConnection() { var connection = new SqlConnection(sqlconnectionString); connection.Open(); return connection; } |
5、查询数据
1 2 3 | var conn = SqlConnection(); string sql = "select * from [User]" ; var list = conn.Query<UserInfo>(sql).ToList(); |
6、构建内存表
1 | if (mac.CustDataBuilder.TableExist( "person" )) //判断内存表名是否存在,存在就移除,避免数据重复<br> {<br> mac.CustDataBuilder.RemoveTable("person"); <br> }<br> mac.CustDataBuilder.NewTable("person");<br> mac.CustDataBuilder.AddStringField("person", "Account", 20);<br> mac.CustDataBuilder.AddStringField("person", "RealName", 30);<br> mac.CustDataBuilder.OpenTable("person"); |
7、内存表中添加数据
1 2 3 4 5 6 | <br> //待解决问题:清空内存表数据 <br> foreach (var item in list)//将查询到的数据循环添加到内存表中 { mac.CustDataBuilder.AppendEmptyRow( "person" ); mac.CustDataBuilder.SetFieldValue( "person" , "Account" , item.Account); mac.CustDataBuilder.SetFieldValue( "person" , "RealName" , item.RealName); }<br> <br> string path = ReportPath + @"\reports\user.apt" ; //设置数据模板路径<br> mac.ShowDesigner();//调用设计器,设计模板<br> mac.LoadFromFile(path);//加载模板<br> mac.Preview();//预览模板 |
分类: C#