2012年8月13日 星期一

Sharepoint BDC 外部清單 下載 Excel

好不容易在整理好 BDC 後才發現他無法使用內建的功能來下載清單到 EXCEL 中.上網找了很久都找到2007 的作法. 那些API 有很多都是已停用的(雖然有人試出來,但我沒有 :( )

好了.以下是這個程式碼

<%@ Assembly Name="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>  
<%@ Assembly Name="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<%@ Assembly Name="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="Microsoft.BusinessData, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ WebHandler Language="C#" Class="ExcelDumper"  %>
using System;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SharePoint;

using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Infrastructure;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.Administration;
using System.Web;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;
public class ExcelDumper : IHttpHandler, IReadOnlySessionState
{
    public bool IsReusable
    {
        get { return false; }
    }
    public void ProcessRequest(HttpContext context)
    {
       
        //entityNS = "GW_BDCM_IP.IP_ALL";
        //string entityName = "ENT_IP_ALL_";/
        string siteURL = "http://spcenter/IP/";
        string entityNS = "GW_BDCM_IP.IP_ALL";// "<nameSpace>";
        string entityName = "ENT_IP_ALL";// "<entityName>";
        string methodInstanceName = "ReadList";//"<methodInstanceName>";
        string H_HTML = "", TB_HTML = "", B_HTML = "";
        bool firtLine = true;
        context.Response.ContentType = "application/vnd.ms-excel";
       
        //context.Response.ContentType =
        //header('Content-disposition: attachment; filename='.$q);
       
        if (context.Request["entityNS"] != null)
        {
            entityNS = context.Request["entityNS"];
        }
        if (context.Request["entityName"] != null)
        {
            entityName = context.Request["entityName"];
        }
        if (context.Request["methodInstanceName"] != null)
        {
            methodInstanceName = context.Request["methodInstanceName"];
        }
       
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (new Microsoft.SharePoint.SPServiceContextScope(
                SPServiceContext.GetContext(site)))
            {
                BdcService service = SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
                IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
                IEntity entity = catalog.GetEntity(entityNS, entityName);
                IMethodInstance imethInst = entity.GetMethodInstance(methodInstanceName, MethodInstanceType.Finder);
                IEntityInstanceEnumerator prodEntityInstanceEnumerator = entity.FindFiltered(imethInst.GetFilters(), entity.GetLobSystem().GetLobSystemInstances()[0].Value);
              
                context.Response.Headers.Add("Content-Disposition", "attachment; filename='Download.xls'");
               
                while (prodEntityInstanceEnumerator.MoveNext())
                {
                    IEntityInstance IE = prodEntityInstanceEnumerator.Current;
                    foreach (IField f in entity.GetFinderView(methodInstanceName).Fields)
                    {
                        if (firtLine)
                        {
                            H_HTML = string.Format("{0}<td>{1}</td>", H_HTML, f.DefaultDisplayName );
                        }
                        TB_HTML = string.Format("{0}<td>{1}</td>", TB_HTML, IE[f].ToString().Replace("\r\n", "<br style=\"mso-data-placement:same-cell;\" />"));
                    }
                    B_HTML = string.Format("{0}<tr>{1}</tr>", B_HTML, TB_HTML);
                    if (firtLine)
                    {
                        B_HTML = string.Format("<tr>{0}</tr>{1}", H_HTML, B_HTML);
                    }
                    TB_HTML = "";
                    firtLine = false;
                }
                B_HTML = string.Format("<table>{0}</table>", B_HTML);
                context.Response.Write(B_HTML);
            }
        }
    }
}

使用方式
1.將這個代碼 存到 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS 目錄下. 並存成 .ashx 檔
2. 自己在網站中建立一個LINK 網址設為
 http://spcenter/IP/_layouts/excel02.ashx?entityNS=GW_BDCM_IP.IP_ALL&entityName=ENT_IP_ALL
參數內容自己看.

沒有留言:

張貼留言