[英]Read excel file stored in SharePoint Document
我有一个 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# 代码中获得它。
如果确定指定的查询确实有效,则根据首选项,至少可以使用以下.NET组件:
下面提供了使用基于WebClient类的 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");
HTML 格式不再被 Excel 客户端支持。 尝试使用对我有用的原子格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.