简体   繁体   English

读取存储在SharePoint文档库中的Excel文件

[英]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服务应用程序

Prepare excel data 准备Excel数据

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库。

在此处输入图片说明

How to read excel data via Excel Services 2010 REST API 如何通过Excel Services 2010 REST API读取Excel数据

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");

References 参考文献

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;

Here is the example of using OpenXML 这是使用OpenXML的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取存储在SharePoint Document中的excel文件 - Read excel file stored in SharePoint Document 使用C#从Sharepoint 2007文档库中打开Excel文件 - Open excel file from Sharepoint 2007 Document Library with C# 使用Sharepoint文档库和Microsoft Graph API处理Excel文件 - Work with Excel file using Sharepoint document library with Microsoft Graph API 使用CSOM打开和读取位于SharePoint文档库中的文本文件 - Open and Read a text file Located in a SharePoint Document Library using CSOM 从存储在Sharepoint中的Excel文档中读取数据? - Reading data from an Excel document stored in Sharepoint? 无法将更改保存到存储在Sharepoint 2010文档库中的XML文档 - Unable to save changes to XML document stored in Sharepoint 2010 Document Library 如何将文件上传到sharepoint文档库 - How to upload file to sharepoint document library 如何将文件上传到Sharepoint中的文档库? - How to upload a file to a document library in sharepoint? 获取保存在Sharepoint文档库中的文件的路径 - Getting path of the file that saved on Sharepoint Document library 如何在 C# 中读取 SharePoint 在线文档中存在的 excel 文件,然后我必须加密一些列,最后将其写入 Snowflake 数据库? - How to read an excel file present in SharePoint online document in C#, then I have to encrypt some columns and finally write it to Snowflake database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM