如何在CRM系統中集成ActiveReports最終報表設計器

来源:http://www.cnblogs.com/powertoolsteam/archive/2016/05/26/EndUserDesigner.html
-Advertisement-
Play Games

有時候,將設計器集成到業務系統中,為用戶提供一些自定義的數據表,用戶不需要瞭解如何底層的邏輯關係和後臺代碼,只需要選擇幾張關聯的數據表,我們會根據用戶的選擇生成可供用戶直接使用的數據集。本文第一部分主要講解了,如何構造三種報表模板,第二部分主要講解了對於DataRelation類在動態綁定數據集之間 ...


有時候,將設計器集成到業務系統中,為用戶提供一些自定義的數據表,用戶不需要瞭解如何底層的邏輯關係和後臺代碼,只需要選擇幾張關聯的數據表,我們會根據用戶的選擇生成可供用戶直接使用的數據集。本文第一部分主要講解了,如何構造三種報表模板,第二部分主要講解了對於DataRelation類在動態綁定數據集之間的用法。

首先需要構造LayoutBuilder類,該類主要構造三種類型的報表以及為它們添加數據集欄位。

 

image

1. 創建RDL 報表

 

使用PageReport對象,並添加屬性

 public static void BuildRdlReportLayout(Design.Designer designer)
        {
            PageReport report = new PageReport();

            report.Report.Body.Height = "5cm";
            report.Report.Width = "20cm";   

            report.Load(new StringReader(report.ToRdlString()));

            //report = LayoutBuilder.AddDataSetDataSource(report);//Adding DataSources to the PageReport object
            report = LayoutBuilder.addDataSet(report);
            MemoryStream reportStream = LayoutBuilder.LoadReportToStream(report);//Loading the PageReport object to a stream
            reportStream.Position = 0;


            designer.LoadReport(XmlReader.Create(reportStream), DesignerReportType.Page);
        }

 

2. 創建頁面報表

 

頁面報表使用頁面報表的構造字元串的方式來創建,否則會預設生成RDL報表

 // 創建頁面報表
        public static void BuildPageReportLayout(Design.Designer designer)
        {
            PageReport report = new PageReport();
            report.Load(new StringReader(
              @"<Report xmlns=""http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"">
                <Body>
                    <Height>27.9cm</Height>
                    <ReportItems>
                        <FixedPage Name=""FixedPage1"">
                            <Pages>
                                <Page />
                            </Pages>
                        </FixedPage>
                    </ReportItems>
                </Body>
                <BottomMargin>2.5cm</BottomMargin>
                <LeftMargin>2.5cm</LeftMargin>
                <PageHeight>27.9cm</PageHeight>
                <PageWidth>21.6cm</PageWidth>
                <RightMargin>2.5cm</RightMargin>
                <TopMargin>2.5cm</TopMargin>
                <Width>21.6cm</Width>
                  </Report>"));

            report = LayoutBuilder.AddDataSetDataSource(report);//Adding DataSources to the PageReport object
            MemoryStream reportStream = LayoutBuilder.LoadReportToStream(report);//Loading the PageReport object to a stream
            reportStream.Position = 0;

            designer.LoadReport(XmlReader.Create(reportStream), DesignerReportType.Page);
        }

 

 

3. 頁面報表和RDL添加數據方法

 

因為頁面報表和RDL報表從代碼級別而言,是相同的,都被視為PageReport,所以對於頁面報表和RDL報表執行的類和介面也都相同。

添加數據集採用的是綁定預先設計的欄位,因為在使用過程中用戶只需要使用現成的數據來設計報表,所以我們需要為報表預先綁定好可以使用的欄位,代碼如下:

 public static PageReport AddDataSetDataSource(PageReport report)
        {
            // create DataSource for the report
            DataSource dataSource = new DataSource();
            dataSource.Name = "Reels Database";
            dataSource.ConnectionProperties.DataProvider = "DATASET";
            dataSource.ConnectionProperties.ConnectString = "";

            //Create DataSet with specified query and load database fields to the DataSet
            DataSet dataSet = new DataSet();
            Query query = new Query();
            dataSet.Name = "Sample DataSet";
            query.DataSourceName = "Reels Database";
            query.CommandType = QueryCommandType.Text;
            query.CommandText = "";
            dataSet.Query = query;
            String[] fieldsList = new String[] { "MoviedID", "Title", "YearReleased", "MPAA" };
            foreach (string fieldName in fieldsList)
            {
                Field field = new Field(fieldName, fieldName, null);
                dataSet.Fields.Add(field);
            }
            //create report definition with specified DataSet and DataSource
            report.Report.DataSources.Add(dataSource);
            report.Report.DataSets.Add(dataSet);
            return report;
        }

 

 

 

4. 創建區域報表及添加數據:

 

區域報表的創建方式與頁面報表類似,採用字元串讀取的形式,並直接添加數據欄位。

 public static void BuildSectionReportLayout(Design.Designer designer)
        {
            // 空白區域報表
            string rpx = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <ActiveReportsLayout Version=\"3.2\" PrintWidth=\"9360\" DocumentName=\"ActiveReports Document\" ScriptLang=\"C#\" MasterReport=\"0\">                              <StyleSheet>                                <Style Name=\"Normal\" Value=\"font-family: Arial; font-style: normal; text-decoration: none; font-weight: normal; font-size: 10pt; color: Black; text-align: left; vertical-align: top; ddo-char-set: 1\" />                                <Style Name=\"Heading1\" Value=\"font-family: Arial; font-size: 16pt; font-style: normal; font-weight: bold\" />                                <Style Name=\"Heading2\" Value=\"font-family: Times New Roman; font-size: 14pt; font-style: italic; font-weight: bold\" />                                <Style Name=\"Heading3\" Value=\"font-family: Arial; font-size: 13pt; font-style: normal; font-weight: bold\" />                              </StyleSheet>                              <Sections>                                <Section Type=\"PageHeader\" Name=\"PageHeader1\" Height=\"360\" BackColor=\"16777215\" />                                <Section Type=\"Detail\" Name=\"Detail1\" Height=\"2880\" BackColor=\"16777215\" />                                <Section Type=\"PageFooter\" Name=\"PageFooter1\" Height=\"360\" BackColor=\"16777215\" />                              </Sections>                              <ReportComponentTray />                              <PageSettings />                              <Parameters />                            </ActiveReportsLayout>";

            // 區域報表數據源結構
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Columns.Add("Col3");

            designer.Report = null;

            // 載入區域報表到設計器
            designer.LoadReport(XmlReader.Create(LayoutBuilder.CovertStringToStream(rpx)), DesignerReportType.Section);

            // 設置區域報表數據源
            SectionReport sr2 = designer.Report as SectionReport;
            sr2.DataSource = dt;
        }

 

5. 修改ReportsForm_Load 方法

 LayoutBuilder.BuildRdlReportLayout(this.reportDesigner);

這樣打開設計器之後,則會呈現帶欄位的數據集,當用戶可以直接使用數據集欄位,設計滿足需求的報表了,當用戶設計完成可能需要預覽報表,這樣就需要為報表返回實際的數據了。

 

首先在報表設計器中添加“預覽”按鈕

 
 

image

 
        // 添加預覽報表菜單
            fileMenu.DropDownItems.Add(new ToolStripMenuItem("預覽", null, new EventHandler(OnViewReport)));
 

實現預覽方法,將當前設計的報表傳給ReportViewer:

 
 private void OnViewReport(object sender, EventArgs e)
        {
            ReportViewer viewer = new ReportViewer();
            viewer.Report = reportDesigner.Report;
            viewer.ReportType = reportDesigner.ReportType;
            viewer.ShowDialog();
        }
 
 
獲取到保鏢對象後,通過LocateDataSource來綁定欄位:
switch (ReportType)
            {
                case DesignerReportType.Page:
                    PageReport report1 = Report as PageReport;
                   
                    viewer1.LoadDocument(report1.Document);
                    break;
                case DesignerReportType.Rdl:
                    PageReport report2 = Report as PageReport;
                    PageDocument reportD = new PageDocument(report2);
                    reportD.LocateDataSource += new LocateDataSourceEventHandler(LoadDataSet);
                    viewer1.LoadDocument(reportD);
                    break;
        
 
其中我們在DataSet中使用了DataRelation 對象,用來創建Table之間的關係,但是AR對DataRelation的支持只限於父級數據的訪問。 

訪問父數據表的欄位時,欄位的首碼應該為合適的數據表的關係名稱,使用“.”進行分割。舉例說明,有一個數據表OrderDetails作為子表關聯到數據表Orders,兩個數據表之間的關係名稱為Orders_OrderDetails。可以使用下麵的語法訪問父數據表的欄位OrderDate:Orders_OrderDetails.OrderDate

使用同樣的語法可以訪問嵌套多層的數據表欄位。如,上例中的數據表Orders也存在父數據表Customers,關係名稱為Customers_orders。命令行文本中指定數據表OrderDetails為主表,使用下麵的語法訪問父數據表的欄位CustomerName:Customers_Orders.Orders_OrderDetails.CustomerName

註:當欄位名稱和關係使用相同名稱時會發生錯誤,暫時不支持。

 
private void LoadDataSet(object sender, LocateDataSourceEventArgs args)
        {
           string constr = @"Provider=sqloledb; password=xA123456;data source=10.32.2.28;initial catalog=NWind_CHS;user id=sa;";
           
            //此處修改為AR的測試資料庫

           // 創建DataSet
           DataSet myDataSet = new DataSet();
           //string connStr = Properties.Resources.ConnectionString;
           OleDbConnection conn = new OleDbConnection(constr);
           DataTable[] myDataTables = new DataTable[3];
           myDataTables[0] = new DataTable(Constants.SaleTableName);
           myDataTables[1] = new DataTable(Constants.SaleDetailsTableName);
           myDataTables[2] = new DataTable(Constants.CustomerTableName);
           
            //創建DataTable    
           myDataSet.Tables.Add(myDataTables[0]);
           OleDbCommand cmd1 = new OleDbCommand(Constants.cmdText1, conn);
           OleDbDataAdapter oleAdapter1 = new OleDbDataAdapter(cmd1);
           oleAdapter1.Fill(myDataSet.Tables[0]);

           //為Table 添加數據
           myDataSet.Tables.Add(myDataTables[1]);
           OleDbCommand cmd2 = new OleDbCommand(Constants.cmdText2, conn);
           OleDbDataAdapter oleAdapter2 = new OleDbDataAdapter(cmd2);
           oleAdapter2.Fill(myDataSet.Tables[1]);

           //為Table 添加數據
           myDataSet.Tables.Add(myDataTables[2]);
           OleDbCommand cmd3 = new OleDbCommand(Constants.cmdText3, conn);
           OleDbDataAdapter oleAdapter3 = new OleDbDataAdapter(cmd3);
           oleAdapter3.Fill(myDataSet.Tables[2]);


           //創建 “DataRelation Customers_Orders”
           DataRelation Customers_Orders = new DataRelation("C_O",myDataSet.Tables[2].Columns["客戶ID"],
               myDataSet.Tables[0].Columns["客戶ID"] );
           myDataSet.Relations.Add(Customers_Orders);
           
           //創建 “DataRelation ”
           DataRelation Orders_OrderDetails = new DataRelation("O_OD ", myDataSet.Tables[0].Columns["訂單ID"],
                              myDataSet.Tables[1].Columns["訂單ID"]);
           myDataSet.Relations.Add(Orders_OrderDetails);
     


          
          
            //返回從表數據
            args.Data = myDataSet.Tables[0];
 
 

圖標預警

 
 
到這裡本篇內容就已經講述完了,主要對於開發CRM系統的一些用戶數據處理,和如何綁定到報表中進行了講述,希望對大家有所幫助。
 
 Demo 下載



您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 說一下剛學習uCOS的心得1)首先強調一下實時操作系統(RTOS)的特點,最明顯的是提供及時響應和高可靠性2)基於實施操作系統的應用程式設計中,其中很重要的一個概念是"任務",任務設計也就是任務函數的設計是整個整 個應用程式的基礎,其他軟體設計工作都是圍繞來展開的3)用戶任務函數中,必須包含至少一次 ...
  • ...
  • serdel是什麼 userdel 是一個底層用於刪除用戶的工具。在 Debian 上,我們通常會使用 deluser 命令。userdel 會查詢系統賬戶文件,例如 /etc/password 和 /etc/group。那麼它會刪除所有和用戶名相關的條目。在我們刪除它之前,用戶名必須存在。 如何使 ...
  • 預熱組件下載地址:下載地址 IIS預熱模塊配置界面插件:下載地址 Warm Up設定方式: (1) 應用程式池層級:只要有需要的應用程式池的Start Mode設定AlwaysRunning就可以 (2) 站點層級:選擇你們要做預熱的站點 通過以上兩步,就完成了warm up, 簡單吧。(IIS8 ...
  • - 什麼是性能調優?(what) - 為什麼需要性能調優?(why) - 什麼時候需要性能調優?(when) - 什麼地方需要性能調優?(where) - 什麼人來進行性能調優?(who) - 怎麼樣進行性能調優?(How) - 總結 # 什麼是性能調優?(what) # ![](http://i. ...
  • 1(1)庫文件:靜態庫文件 和 共用庫文件(2)比較a.靜態庫文件: 使用靜態庫文件時,直接把代碼/指令複製到目標文件中 目標文件會顯得比較龐大,修改和維護都不方便 可以脫離靜態庫文件,效率比較高 b.共用庫文件: 使用共用庫時,將代碼/指令所對應的地址複製到目標文件 目標文件會比較小,修改和維護比 ...
  • 最近由於項目需要,需要打開防火牆功能. 公司有 arm linux 3.0x86 linux 3.2x86 linux 2.4 的三個嵌入式.都需要打開防火牆功能. 執行“whereis iptables”命令,如果結果不為空,則說明防火牆軟體已安裝 輸入iptables -L 命令查看配置 此處為 ...
  • [源碼下載] 背水一戰 Windows 10 (14) - 動畫: 線性動畫, 關鍵幀動畫 作者:webabcd介紹背水一戰 Windows 10 之 動畫 線性動畫 - ColorAnimation, DoubleAnimation, PointAnimation 關鍵幀動畫 - ColorAni ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...