65.9K
CodeProject 正在变化。 阅读更多。
Home

将 BDC(业务数据目录)数据导出到 Excel/PDF

2009年7月24日

CPOL

2分钟阅读

viewsIcon

85734

downloadIcon

591

将搜索结果(BDC 数据列表/企业搜索)导出到 Excel/PDF 的方法

引言

MOSS 2007 中最酷的功能之一是业务数据目录,它提供了一种简单的方法,可以将来自后端服务器应用程序(例如 SAP 或 Siebel)的多个业务数据与您的企业门户集成,从而为最终用户提供丰富的解决方案,而无需编写任何代码。它就像一个可互操作的解决方案,您可以将任何其他数据源集成到 MOSS 2007 环境中。您通过创建描述数据库或 Web 服务的元数据,在业务数据目录中注册在数据库中或通过 Web 服务公开的业务数据。然后,业务数据目录使用此元数据来正确调用数据源以检索相关数据。

背景

客户最热门的需求之一是他们希望从 BDC 导出其搜索结果。业务数据实体可供以下任何业务数据功能使用:业务数据 Web 部件、业务数据列表、业务数据搜索以及用户配置文件中的业务数据。因此,客户希望将这些业务数据实体导出到 Excel 或 PDF。但不幸的是,Microsoft 没有提供任何工具来导出到任何格式。因此,我在这里提出一种将业务数据实体导出到 Excel/PDF 的方法。

Using the Code

有两种方法可以将业务实体导出到 Excel

  1. 使用 HTTPHandler
  2. 使用通用调用器(通常,当您进行自定义 Web 部件开发时,此选项会很有帮助)

当您想从其他系统(即 SAP R/3、PeopleSoft)导出企业搜索结果(使用业务数据开发)和用户配置文件信息时,第一个选项将非常有用。

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/vnd.ms-excel";
    NamedLobSystemInstanceDictionary ObjInstances = 
		ApplicationRegistry.GetLobSystemInstances();
    LobSystemInstance ObjInstance = ObjInstances["Give your Instance Name"];
    Entity ObjEntity = ObjInstance.GetEntities()["Give your Entity Name"];
    MethodInstance ObjMethodInst = ObjEntity.GetFinderMethodInstance();
    IEntityInstanceEnumerator ObjEntityInstanceEnumerator = 
	(IEntityInstanceEnumerator)prodEntity.Execute(ObjMethodInst, ObjInstance);
    while (ObjEntityInstanceEnumerator.MoveNext())
    {
        IEntityInstance IE = prodEntityInstanceEnumerator.Current;
        foreach (Field f in prodEntity.GetFinderView().Fields)
        {
            context.Response.Write(IE[f]);
            context.Response.Write('\t');
        }
        context.Response.Write('\n');
    }
}

第二个选项是调用 GenericInvoker 来执行 MethodInstance,然后将返回结果导出到 Excel,如下所示:

//Application Definition

        <Method Name="ExportExcel">
          <Properties>
            </Parameter>
          </Parameters>
          <MethodInstances>
            <MethodInstance Name="ExportToExcel" 
		Type="GenericInvoker" ReturnParameterName="ExportPlantsExcel"/>
          </MethodInstances>
        </Method> 
//WebPart
        protected override void CreateChildControls()
        {
            lbExcel = new HyperLink();
            lbExcel.Text = "Export To Excel";
            lbExcel.NavigateUrl = SPContext.Current.Web.Url + 
			@"/Export.ashx?format=excel&instance=ExcelInstance";
            lbExcel.Load += new EventHandler(lbExcel_Load);
            lbExcel.ImageUrl = "/_layouts/images/ICXLS.GIF";
 
            lbPdf = new HyperLink();
            lbPdf.Text = "Export To PDF";
            lbPdf.Load += new EventHandler(lbPdf_Load);
            lbExcel.NavigateUrl = SPContext.Current.Web.Url + 
			@"/Export.ashx?format=pdf&instance=PDFInstance";
            lbExcel.ImageUrl = "/_layouts/images/pdf.gif";
           
                ….
        }

session 对象必须保存来自 GenericInvoker MethodInstance 的对象结果

        /// <summary>
        /// Holds PDF Instance
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void lbPdf_Load(object sender, EventArgs e)
        {
            System.Web.HttpContext.Current.Session["PDFInstance"] =
		BdcHelpers.ExecuteGenericInvoker(lobSystemInstance, 
		entityName, "ExportToExcel");
        }
 
        /// <summary>
        /// Holds Excel Instance
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>     
        void lbExcel_Load(object sender, EventArgs e)
        {
           System.Web.HttpContext.Current.Session["ExcelInstance"] =
		Helpers.ExecuteGenericInvoker(lobSystemInstance, 
		entityName, "ExportToExcel");
        }
        /// <summary>
        /// Overloads Execute Method for MethodInstance of specified LOBSystem
        /// </summary>
        /// <param name="lobSystemInstance">LOB System</param>
        /// <param name="entityName">Name Of an Enity</param>
        /// <param name="methodInstance">GenericInvoker Method Instance Name</param>
        /// <returns>Object</returns>
        public static Object ExecuteGenericInvoker
	(string lobSystemInstance, string entityName, stringmethodInstance)
        {
            NamedLobSystemInstanceDictionary instances = 
			ApplicationRegistry.GetLobSystemInstances();
            LobSystemInstance instance = instances[lobSystemInstance];
            Entity entity = instance.GetEntities()[entityName];
 
            MethodInstance methInst = entity.GetMethodInstances()[methodInstance];
 
           return entity.Execute(methInst, instance);
        }

最后,您必须注册您的处理程序,如下所示

<httpHandlers>
    <remove verb="GET,HEAD,POST" path="*" />
    <add verb="*" path="Export.ashx" type="BDCWebParts.ExportHandler, BDCWebParts" />
</httpHandlers> 

有关更多详细信息,请访问我的博客 http://sharepointblogs.com/lovedjohnysmith

历史

  • 2009年7月24日:首次发布
© . All rights reserved.