简体   繁体   中英

Read excel file stored in SharePoint Document

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

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.

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

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;

}

Usage

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

HTML format is no more supported by Excel client. Try using atom format which worked for me

https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/hh124646(v=office.14)?redirectedfrom=MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM