c# - Read excel file stored in SharePoint Document -
i have excel file stored in sharepoint document library. need read excel file , data programmatically 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 keep link in browser, desired value in html. now, how can in c# code.
if specified query works, depending on preferences @ least following .net components available:
- httpwebrequest class
- webclient class
- httpclient class (.net >= 4.5)
below provided example 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");
Comments
Post a Comment