读取存储在SharePoint Document中的Excel文件

时间:2015-04-01 13:41:10

标签: c# asp.net sharepoint

我有一个存储在SharePoint文档库中的Excel文件。我需要读取此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#代码中获取它。

1 个答案:

答案 0 :(得分:0)

如果您确定指定的查询确实有效,那么根据首选项,至少可以使用以下.NET组件:

下面提供了基于WebClient Class消费SharePoint Excel REST服务的示例。

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");