读取存储在 SharePoint 文档中的 excel 个文件
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";
在这里,当我在浏览器中保留这个 link 时,我在 HTML 中得到了想要的值。现在,我怎样才能在 C# 代码中得到它。
如果您确定指定的查询确实有效,那么根据首选项至少可以使用以下 .NET 组件:
- HttpWebRequest class
- WebClient Class
- HttpClient Class (.NET >= 4.5)
下面提供了使用基于 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");
HTML 格式不再被 Excel 客户端支持。尝试使用对我有用的 atom 格式
我有一个 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";
在这里,当我在浏览器中保留这个 link 时,我在 HTML 中得到了想要的值。现在,我怎样才能在 C# 代码中得到它。
如果您确定指定的查询确实有效,那么根据首选项至少可以使用以下 .NET 组件:
- HttpWebRequest class
- WebClient Class
- HttpClient Class (.NET >= 4.5)
下面提供了使用基于 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");
HTML 格式不再被 Excel 客户端支持。尝试使用对我有用的 atom 格式