1、引用AcReport.dll组件
2、注册组件
/// <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组件
ReportPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath); mac = new AcReport.AcRptEngine(); SetAcRegisterInfo();
实例化一个AcRptEngine对象,AcReport中只有一个实例化对象AcRptEngine。
4、定义dapper访问数据库
static string sqlconnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();//获取App.config中的连接字符串 public static SqlConnection SqlConnection() { var connection = new SqlConnection(sqlconnectionString); connection.Open(); return connection; }
5、查询数据
var conn = SqlConnection(); string sql = "select * from [User]"; var list = conn.Query<UserInfo>(sql).ToList();
6、构建内存表
if (mac.CustDataBuilder.TableExist("person"))//判断内存表名是否存在,存在就移除,避免数据重复
{
mac.CustDataBuilder.RemoveTable("person");
}
mac.CustDataBuilder.NewTable("person");
mac.CustDataBuilder.AddStringField("person", "Account", 20);
mac.CustDataBuilder.AddStringField("person", "RealName", 30);
mac.CustDataBuilder.OpenTable("person");
7、内存表中添加数据
//待解决问题:清空内存表数据
foreach (var item in list)//将查询到的数据循环添加到内存表中 { mac.CustDataBuilder.AppendEmptyRow("person"); mac.CustDataBuilder.SetFieldValue("person", "Account", item.Account); mac.CustDataBuilder.SetFieldValue("person", "RealName", item.RealName); }
string path = ReportPath + @" eportsuser.apt";//设置数据模板路径
mac.ShowDesigner();//调用设计器,设计模板
mac.LoadFromFile(path);//加载模板
mac.Preview();//预览模板