2015-04-01 44 views
0

我有一個存儲在SharePoint文檔庫中的excel文件。我需要讀取這個excel文件並從文件路徑中以編程方式獲取數據。閱讀存儲在SharePoint文檔中的excel文件

string rangeName = "Sheet1!D43"; 
      string value = GetRangeValue("abc.xslx", rangeName); 
string url = 
      "http://sharepoint1.net/excel/_vti_bin/ExcelRest.aspx/docs/" + 
       workbookName + "/model/Ranges('" + rangeName + "')?$format=HTML"; 

這裏,當我在瀏覽器中保留這個鏈接時,我得到了HTML中所需的值。現在,我怎樣才能在C#代碼中獲得它。

回答

0

如果您確定指定的查詢確實有效,則根據至少下列.NET組件可用的首選項:

下面提供了基於的消費SharePoint Excel REST服務的示例3210。

public class ExcelClient : IDisposable 
{ 

    public ExcelClient(Uri webUri, ICredentials credentials) 
    { 
     WebUri = webUri; 
     _client = new WebClient {Credentials = credentials}; 
    } 


    public string ReadRange(string libraryName, string fileName,string rangeName, string formatType) 
    { 
     var endpointUrl = WebUri + string.Format("/_vti_bin/ExcelRest.aspx/{0}/{1}/Model/Ranges('{2}')?$format={3}", libraryName,fileName, rangeName, formatType); 
     return _client.DownloadString(endpointUrl); 
    } 


    public void Dispose() 
    { 
     _client.Dispose(); 
     GC.SuppressFinalize(this); 
    } 

    public Uri WebUri { get; private set; } 

    private readonly WebClient _client; 

} 

使用

var credentials = new NetworkCredential(userName,password,domain); 
var client = new ExcelClient(webUri, credentials); 
var data = client.ReadRange("docs", workbookName, rangeName, "html"); 
+0

你能告訴我,這可能是我的webUri? 我在此位置獲得價值:** http://sharepoint1.net/excel/_vti_bin/ExcelRest.aspx/docs/「+ workbookName +」/ model/Ranges('「+ rangeName +」')?$ format = HTML ** – hello 2015-04-01 14:12:50

+0

當然,這是網站url:http://sharepoint1.net/excel在你的情況下(我假設'excel'是'docs'庫駐留在哪個excel文件'abc的子站點的名稱.xslx'被存儲) – 2015-04-01 14:16:02

+0

我得到錯誤:**遠程服務器返回錯誤:(400)錯誤的請求。**。我在'return _client.DownloadString(endpointUrl)';' – hello 2015-04-01 14:33:54