如何使用控制台应用程序将sharepoint文档库中的Excel文件下载到本地文件夹。 [英] How can I download the Excel Files present in my sharepoint document library to my local folder using a console application.
本文介绍了如何使用控制台应用程序将sharepoint文档库中的Excel文件下载到本地文件夹。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
注意:我希望批量下载而不是文档库中的单个文件。
这是我现在使用的代码但是它给我一些错误,如未授权状态代码401
class 计划
{
静态 void Main( string [] args)
{
// 给excel文件路径。
string filePath = exel文件路径;
DownloadXLFile(filePath, my path);
}
public static void DownloadXLFile( string strPath, string strDest)
{
try
{
ExcelService xlService = new ExcelService();
xlService.Url = http://mysiteurl/_vti_bin/ExcelService.asmx;
System.Net.CredentialCache.DefaultNetworkCredentials.UserName = username;
System.Net.CredentialCache.DefaultNetworkCredentials.Password = password;
System.Net.CredentialCache.DefaultNetworkCredentials.Domain = http:// siteurl / path;
// xlService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
状态[]状态;
string sessionId = xlService.OpenWorkbook(strPath, String .Empty, String .Empty, out status); // 此处我遇到异常
byte [] workbook = xlService.GetWorkbook(sessionId,WorkbookType.FullWorkbook, out status);
FileStream fs = new FileStream(strDest,FileMode.Create);
fs.Write(工作簿, 0 ,workbook.Length);
fs.Dispose();
}
catch (例外情况)
{
throw new 异常( CreateFile - + ex.Message);
}
}
}
解决方案
您传递网络凭证的方式是错误的。您是需要像这样传递它
xlService.Credentials = new NetworkCredential (用户名密码);
例如: - xlService.Credentials = new NetworkCredential( myName, myPassword);
Note : I want a bulk download not a single file from document library.
Here is the code I am using now but it is throwing me some error like "Unauthorized with status code 401"
class Program
{
static void Main(string[] args)
{
// give the excel file path.
string filePath = "exel file path";
DownloadXLFile(filePath, "my path");
}
public static void DownloadXLFile(string strPath, string strDest)
{
try
{
ExcelService xlService = new ExcelService();
xlService.Url = "http://mysiteurl/_vti_bin/ExcelService.asmx";
System.Net.CredentialCache.DefaultNetworkCredentials.UserName = "username";
System.Net.CredentialCache.DefaultNetworkCredentials.Password = "password";
System.Net.CredentialCache.DefaultNetworkCredentials.Domain = "http://siteurl/path";
//xlService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Status[] status;
string sessionId = xlService.OpenWorkbook(strPath, String.Empty, String.Empty, out status); // Here iam getting exception
byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, out status);
FileStream fs = new FileStream(strDest, FileMode.Create);
fs.Write(workbook, 0, workbook.Length);
fs.Dispose();
}
catch (Exception ex)
{
throw new Exception("CreateFile - " + ex.Message);
}
}
}
解决方案
The way you are passing network credentils is wrong .You need to pass it like this
xlService.Credentials = new NetworkCredential(username,password); e.g:- xlService.Credentials = new NetworkCredential("myName","myPassword");
这篇关于如何使用控制台应用程序将sharepoint文档库中的Excel文件下载到本地文件夹。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文