读取Excel存储在SharePoint文档文件 [英] Read excel file stored in SharePoint Document

查看:530
本文介绍了读取Excel存储在SharePoint文档文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在SharePoint文档库中的Excel文件。我需要阅读这个Excel文件,并从文件路径以编程方式获取数据。

I have an excel file stored in SharePoint document library. I need to read this excel file and get the data Programmatically from the file path.

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#code。

Here, When I keep this link in browser, I get the desired value in HTML. Now, how can I get it in the c# code.

推荐答案

如果您确信指定的查询确实有效,则取决于preferences至少以下.NET组件:

If you are certain the specified query really works, then depending on the preferences at least the following .NET components are available:

  • HttpWebRequest class
  • WebClient Class
  • HttpClient Class (.NET >= 4.5)

下面提供的示例消费基础上的 WebClient类

Below is provided the example for consuming SharePoint Excel REST services based on WebClient Class.

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

这篇关于读取Excel存储在SharePoint文档文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆