[英]Read excel file stored in SharePoint Document Library
I have an excel file stored in SharePoint document library. 我在SharePoint文档库中存储了一个Excel文件。 I need to read this excel file and get the data programmatically from the file path
我需要读取此excel文件并以编程方式从文件路径获取数据
For Example: SharePoint Site: http:// servername :1000 例如:SharePoint网站:http://服务器名:1000
Excel file path :http://servername:1000/ExcelDocs//ExcelFile.xlsx Excel文件路径:http://服务器名称:1000 / ExcelDocs // ExcelFile.xlsx
As we see I have path to excel file stored in SharePoint document library. 如我们所见,我具有存储在SharePoint文档库中的excel文件的路径。 I need to get data from this file.
我需要从该文件中获取数据。 Any ideas?
有任何想法吗?
Thanks 谢谢
In fact, you could read Excel file content without any third-party components involved, using SharePoint capabilities only. 实际上,仅使用SharePoint功能,您就可以在不涉及任何第三方组件的情况下读取Excel文件内容。
The solution demonstrates how to consume SharePoint REST service to read excel data . 该解决方案演示了如何使用SharePoint REST服务来读取Excel数据 。 Another point about this approach, since there is no any dependencies to SharePoint libraries at all, neither server side nor client side assemblies are referenced.
关于此方法的另一点,因为根本没有对SharePoint库的任何依赖关系,所以服务器端和客户端程序集均未引用。
Prerequisite: Excel Services service application has to be configured
先决条件:必须配置Excel Services服务应用程序
Assume the following excel file 假设以下excel文件
that contains the following items in the workbook: 在工作簿中包含以下各项:
Before uploading file into SharePoint library, go to File -> Browse View Options -> choose Items in the Workbook
as demonstrated below. 在将文件上传到SharePoint库之前,请转到
File -> Browse View Options -> choose Items in the Workbook
如下所示。 Then save file and upload it into SharePoint Documents
library. 然后保存文件并将其上传到SharePoint
Documents
库。
The following example demonstrates how to read excel table content using Excel Services 2010 REST API : 以下示例演示如何使用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;
}
}
Usage 用法
The example demonstrates how to read table content using JSON format: 该示例演示如何使用JSON格式读取表内容:
var credentials = new NetworkCredential(userName,password,domain);
var client = new ExcelClient(webUri, credentials);
var tableData = client.ReadTable("Documents","ciscoexpo.xlsx", "CiscoSalesTable","html");
You can obtain the binary data from SPFile object and then open it in third party library ClosedXML 您可以从SPFile对象获取二进制数据,然后在第三方库ClosedXML中打开它。
SPFile file = web.GetFile("http://servername:1000/ExcelDocs//ExcelFile.xlsx");
Stream dataStream = file.OpenBinaryStream();
XLWorkbook workbook = new XLWorkbook(dataStream);
or you can use OpenXML , which is Microsoft SDK. 或者您可以使用Microsoft XML SDK OpenXML 。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.