我在SharePoint文档库中存储了一个excel文件。我需要读取这个excel文件,并以编程方式从文件路径获取数据。
例如: SharePoint站点:服务器名称:1000
Excel文件路径:http://servername:1000/ExcelDocs//ExcelFile.xlsx
正如我们所看到的,我有存储在SharePoint文档库中的excel文件的路径。我需要从这个文件中获取数据。有什么想法吗?
谢谢
发布于 2015-03-30 02:43:30
实际上,只使用SharePoint功能,就可以在没有任何第三方组件的情况下读取Excel文件内容。
该方案演示了如何使用SharePoint REST服务读取excel数据。关于这种方法的另一点是,由于没有对SharePoint库的任何依赖,所以既不引用服务器端也不引用客户端程序集。
先决条件:必须配置Excel Services service application
准备excel数据
假设下面的excel文件
它包含工作簿中的以下项:
在将文件上传到SharePoint库之前,请转到File -> Browse View Options -> choose Items in the Workbook
,如下所示。然后保存文件并将其上传到SharePoint Documents
库中。
如何通过excel Services 2010 REST API读取Excel数据
下面的示例演示如何使用Excel Services 2010 REST API读取excel表内容
using System;
using System.Net;
namespace SharePoint.Client.Office
{
public class ExcelClient : IDisposable
{
public ExcelClient(Uri webUri, ICredentials credentials)
{
WebUri = webUri;
_client = new WebClient {Credentials = credentials};
}
public string ReadTable(string libraryName,string fileName, string tableName,string formatType)
{
var endpointUrl = string.Format("{0}/_vti_bin/ExcelRest.aspx/{1}/{2}/Model/Tables('{3}')?$format={4}", WebUri,libraryName,fileName, tableName, formatType);
return _client.DownloadString(endpointUrl);
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
public Uri WebUri { get; private set; }
private readonly WebClient _client;
}
}
使用
本示例演示了如何使用JSON格式读取表格内容:
var credentials = new NetworkCredential(userName,password,domain);
var client = new ExcelClient(webUri, credentials);
var tableData = client.ReadTable("Documents","ciscoexpo.xlsx", "CiscoSalesTable","html");
参考文献
发布于 2013-01-24 16:27:18
您可以从SPFile对象中获取二进制数据,然后在第三方库ClosedXML中打开它
SPFile file = web.GetFile("http://servername:1000/ExcelDocs//ExcelFile.xlsx");
Stream dataStream = file.OpenBinaryStream();
XLWorkbook workbook = new XLWorkbook(dataStream);
或者,您可以使用OpenXML,这是Microsoft SDK。
SPFile file = web.GetFile("http://servername:1000/ExcelDocs//ExcelFile.xlsx");
Stream dataStream = file.OpenBinaryStream();
SpreadsheetDocument document = SpreadsheetDocument.Open(dataStream, false);
Workbook workbook = document.WorkbookPart.Workbook;
下面是使用OpenXML的example
https://stackoverflow.com/questions/14496608
复制相似问题