Share via


建立自訂報表專案運行時間元件

自訂報表項目執行階段元件會使用任何符合 CLS 規範的語言實作為 Microsoft .NET Framework 元件,並且在執行階段由報表處理器呼叫。 您可藉由修改自訂報表項目對應的設計階段元件,在設計環境中定義執行階段元件的屬性。

如需完全實作的自訂報表項目的範例,請參閱 SQL Server Reporting Services Product Samples (SQL Server Reporting Services 產品範例)。

定義和實例物件

實作自定義報表專案之前,請務必瞭解定義對象與實例對象之間的差異。 定義物件提供自訂報表項目的 RDL 表示法,而執行個體物件是定義物件的評估版本。 報表上每個專案只有一個定義物件。 在包含表達式的定義物件上存取屬性時,您會取得未評估的表達式字元串。 執行個體物件包含已評估的定義物件版本,而且與項目的定義物件可能具有一對多的關聯性。 例如,如果報表具有 Tablix 資料區,而該資料區在詳細資料列中包含 CustomReportItem,則只會有一個定義物件,但資料區中的每個資料列都會有一個執行個體物件。

實作 ICustomReportItem 介面

若要建立 CustomReportItem 運行時間元件,您需要實 ICustomReportItem 作 Microsoft.ReportingServices.ProcessingCore.dll 中定義的介面:

namespace Microsoft.ReportingServices.OnDemandReportRendering  
{  
    public interface ICustomReportItem  
    {  
        void GenerateReportItemDefinition(CustomReportItem customReportItem);  
void EvaluateReportItemInstance(CustomReportItem customReportItem);  
    }  
}  

實作 ICustomReportItem 介面之後,將會為您產生兩個方法存根: GenerateReportItemDefinitionEvaluateReportItemInstance。 方法 GenerateReportItemDefinition 會先呼叫 ,並用於設定定義屬性,並建立 Image 物件,其中包含用於轉譯專案的定義和實例屬性。 方法 EvaluateReportItemInstance 會在評估定義對象之後呼叫,並提供將用於轉譯項目的實例物件。

下列範例實作顯示將控件名稱轉譯為影像的自定義報表專案。

namespace Microsoft.Samples.ReportingServices  
{  
    using System;  
    using System.Collections.Generic;  
    using System.Collections.Specialized;  
    using System.Drawing.Imaging;  
    using System.IO;  
    using System.Text;  
    using Microsoft.ReportingServices.OnDemandReportRendering;  
  
    public class PolygonsCustomReportItem : ICustomReportItem  
    {  
        #region ICustomReportItem Members  
  
        public void GenerateReportItemDefinition(CustomReportItem cri)  
        {  
            // Create the Image object that will be   
            // used to render the custom report item  
            cri.CreateCriImageDefinition();  
            Image polygonImage = (Image)cri.GeneratedReportItem;  
        }  
  
        public void EvaluateReportItemInstance(CustomReportItem cri)  
        {  
            // Get the Image definition  
            Image polygonImage = (Image)cri.GeneratedReportItem;  
  
            // Create the image for the custom report item  
            polygonImage.ImageInstance.ImageData = DrawImage(cri);  
        }  
  
        #endregion  
  
        /// <summary>  
        /// Creates an image of the CustomReportItem's name  
        /// </summary>  
        private byte[] DrawImage(CustomReportItem customReportItem)  
        {  
            int width = 1;          // pixels  
            int height = 1;         // pixels  
            int resolution = 75;    // dpi  
  
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);  
            bitmap.SetResolution(resolution, resolution);  
  
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);  
            graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;  
  
            // Get the Font for the Text  
            System.Drawing.Font font = new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace,  
                12, System.Drawing.FontStyle.Regular);  
  
            // Get the Brush for drawing the Text  
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen);  
  
            // Get the measurements for the image  
            System.Drawing.SizeF maxStringSize = graphics.MeasureString(customReportItem.Name, font);  
            width = (int)(maxStringSize.Width + 2 * font.GetHeight(resolution));  
            height = (int)(maxStringSize.Height + 2 * font.GetHeight(resolution));  
  
            bitmap.Dispose();  
            bitmap = new System.Drawing.Bitmap(width, height);  
            bitmap.SetResolution(resolution, resolution);  
  
            graphics.Dispose();  
            graphics = System.Drawing.Graphics.FromImage(bitmap);  
            graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;  
  
            // Draw the text  
            graphics.DrawString(customReportItem.Name, font, brush, font.GetHeight(resolution),   
                font.GetHeight(resolution));  
  
            // Create the byte array of the image data  
            MemoryStream memoryStream = new MemoryStream();  
            bitmap.Save(memoryStream, ImageFormat.Bmp);  
            memoryStream.Position = 0;  
            byte[] imageData = new byte[memoryStream.Length];  
            memoryStream.Read(imageData, 0, imageData.Length);  
  
            return imageData;  
        }  
    }  
}  

自訂報表項目架構
建立自訂報表項目設計時間元件
自訂報表項目類別庫
如何:部署自定義報表專案