如何使用Rest API将excel文件从sharepoint下载到我的本地计算机 [英] How to download excel file from sharepoint to my local machine using Rest API

查看:84
本文介绍了如何使用Rest API将excel文件从sharepoint下载到我的本地计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


是否有api可以从中将excel文件从sharepoint下载到本地机器。


你能不能请帮我提供文档。


Noffil Chougle

解决方案


以下是供您参考的示例代码段:

 static void Main(string [] args)
{

string siteURL =" http:// sp / sites / dev" ;;

//在线设置SharePoint凭据
// SecureString secureString = new SecureString();
// foreach(char c in" Password" .ToCharArray())
// {
// secureString.AppendChar(c);
//}
// ICredentials credentials = new SharePointOnlineCredentials(" xxxxxx.onmicrosoft.com",secureString);

//设置SharePoint 2013的凭据(内部部署)
string userName =" Administrator" ;;
string password =" Access1" ;;
string domain =" CONTOSO" ;;
ICredentials credentials = new NetworkCredential(userName,password,domain);

DownloadFileViaRestAPI(siteURL,credentials," MyDocumentLib"," test.xlsx"," C:\\");

Console.WriteLine(" success");
Console.ReadLine();

}
public static void DownloadFileViaRestAPI(string webUrl,ICredentials credentials,string documentLibName,string fileName,string path)
{
webUrl = webUrl.EndsWith(" /")? webUrl.Substring(0,webUrl.Length - 1):webUrl;
string webRelativeUrl = null;
if(webUrl.Split('/')。长度> 3)
{
webRelativeUrl =" /" + webUrl.Split(new char [] {'/'},4)[3];
}
其他
{
webRelativeUrl ="" ;;
}

使用(WebClient客户端=新WebClient())
{
client.Headers.Add(" X-FORMS_BASED_AUTH_ACCEPTED"," f") ;
client.Credentials =凭证;
Uri endpointUri = new Uri(webUrl +" / _ api / web / GetFileByServerRelativeUrl('" + webRelativeUrl +" /" + documentLibName +" /" + fileName +"')/

值");
// string result = client.DownloadString(endpointUri);
byte [] data = client.DownloadData(endpointUri);
FileStream outputStream = new FileStream(path + fileName,FileMode.OpenOrCreate | FileMode.Append,FileAccess.Write,FileShare.None);
outputStream.Write(data,0,data.Length);
outputStream.Flush(true);
outputStream.Close();
}
}


谢谢


最好的问候



Hi,

Is there api available from which i can download excel files from sharepoint to local machine.

Can you please help me with documentation.

Noffil Chougle

解决方案

Hi,

Here is a sample code snippet for your reference:

  static void Main(string[] args)
        {
       
            string siteURL = "http://sp/sites/dev";

            //set credential of SharePoint online
            //SecureString secureString = new SecureString();
            //foreach (char c in "Password".ToCharArray())
            //{
            //    secureString.AppendChar(c);
            //}
            //ICredentials credentials = new SharePointOnlineCredentials("xxxxxx.onmicrosoft.com", secureString);

            //set credential of SharePoint 2013(on-premise)
            string userName = "Administrator";
            string password = "Access1";
            string domain = "CONTOSO";
            ICredentials credentials = new NetworkCredential(userName, password, domain);

            DownloadFileViaRestAPI(siteURL, credentials, "MyDocumentLib", "test.xlsx", "C:\\");

            Console.WriteLine("success");
            Console.ReadLine();

        }
        public static void DownloadFileViaRestAPI(string webUrl, ICredentials credentials, string documentLibName, string fileName, string path)
        {
            webUrl = webUrl.EndsWith("/") ? webUrl.Substring(0, webUrl.Length - 1) : webUrl;
            string webRelativeUrl = null;
            if (webUrl.Split('/').Length > 3)
            {
                webRelativeUrl = "/" + webUrl.Split(new char[] { '/' }, 4)[3];
            }
            else
            {
                webRelativeUrl = "";
            }

            using (WebClient client = new WebClient())
            {
                client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
                client.Credentials = credentials;
                Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + webRelativeUrl + "/" + documentLibName + "/" + fileName + "')/


value"); //string result = client.DownloadString(endpointUri); byte[] data = client.DownloadData(endpointUri); FileStream outputStream = new FileStream(path + fileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None); outputStream.Write(data, 0, data.Length); outputStream.Flush(true); outputStream.Close(); } }

Thanks

Best Regards


这篇关于如何使用Rest API将excel文件从sharepoint下载到我的本地计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆