使用 Interop 传递 SharePoint 凭据以访问 Excel 文件?
Pass SharePoint Credentials to Access Excel File using Interop?
string url = HttpUtility.HtmlAttributeEncode("https://site.sharepoint.com/sites/Project/Shared%20Documents/ExcelSheet.xlsx");
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(url):
这会提示我输入电子邮件(用户 ID)和密码,我知道我可以传递密码,但我如何在其中获取电子邮件(用户 ID)。
我也保存了 SharpePoint 凭据,我最初尝试使用 SharepointClient 但我已经能够下载文件,我无法弄清楚如何将它转换为 Excel 工作簿以遍历单元格。
ClientContext context = new ClientContext("https://site.sharepoint.com/sites/Project/"):
SecureString passWord = new SecureString();
context.Credentials = new SharePointOnlineCredentials(password)
干杯!
而不是 Excel Interop i would propose to utilize Excel Services REST API,它允许读取 excel 数据而不依赖于外部或第三方库。
例子
以下示例演示如何从存储在 SharePoint Online Documents
库 Financial Sample.xlsx
示例 excel 文件中读取 工作簿数据
var credentials = GetCredentials(userName, password);
var client = new ExcelClient(webUrl, credentials);
var data = client.ReadTable("Shared Documents/Financial Sample.xlsx", "Sheet1","A1", "P500");
JObject table = JObject.Parse(data);
int idx = 0;
foreach(var row in table["rows"])
{
if(idx == 0)
{
//skip header
}
else
{
//get cell values
var segment = row[0]["v"] as JValue;
var country = row[1]["v"] as JValue;
Console.WriteLine("Segment: {0}, Country: {1}", segment.Value,country.Value);
}
idx++;
}
哪里
WebClient
class 用于消费 Excel 服务 REST
public class ExcelClient : WebClient
{
public ExcelClient(string webUrl, ICredentials credentials)
{
BaseAddress = webUrl;
Credentials = credentials;
Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}
public string ReadTable(string fileUrl, string sheetName, string cellStart,string cellEnd, string format="json")
{
var endpointUrl = BaseAddress + string.Format("/_vti_bin/ExcelRest.aspx/{0}/Model/Ranges('{1}!{2}|{3}')?$format={4}", fileUrl,sheetName,cellStart,cellEnd,format);
return DownloadString(endpointUrl);
}
}
和 SharePointOnlineCredentials
class 通过用户凭据访问 SharePoint Online 资源
static ICredentials GetCredentials(string userName, string password)
{
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}
return new SharePointOnlineCredentials(userName, securePassword);
}
结果
参考资料
string url = HttpUtility.HtmlAttributeEncode("https://site.sharepoint.com/sites/Project/Shared%20Documents/ExcelSheet.xlsx");
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(url):
这会提示我输入电子邮件(用户 ID)和密码,我知道我可以传递密码,但我如何在其中获取电子邮件(用户 ID)。
我也保存了 SharpePoint 凭据,我最初尝试使用 SharepointClient 但我已经能够下载文件,我无法弄清楚如何将它转换为 Excel 工作簿以遍历单元格。
ClientContext context = new ClientContext("https://site.sharepoint.com/sites/Project/"):
SecureString passWord = new SecureString();
context.Credentials = new SharePointOnlineCredentials(password)
干杯!
而不是 Excel Interop i would propose to utilize Excel Services REST API,它允许读取 excel 数据而不依赖于外部或第三方库。
例子
以下示例演示如何从存储在 SharePoint Online Documents
库 Financial Sample.xlsx
示例 excel 文件中读取 工作簿数据
var credentials = GetCredentials(userName, password);
var client = new ExcelClient(webUrl, credentials);
var data = client.ReadTable("Shared Documents/Financial Sample.xlsx", "Sheet1","A1", "P500");
JObject table = JObject.Parse(data);
int idx = 0;
foreach(var row in table["rows"])
{
if(idx == 0)
{
//skip header
}
else
{
//get cell values
var segment = row[0]["v"] as JValue;
var country = row[1]["v"] as JValue;
Console.WriteLine("Segment: {0}, Country: {1}", segment.Value,country.Value);
}
idx++;
}
哪里
WebClient
class 用于消费 Excel 服务 REST
public class ExcelClient : WebClient
{
public ExcelClient(string webUrl, ICredentials credentials)
{
BaseAddress = webUrl;
Credentials = credentials;
Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}
public string ReadTable(string fileUrl, string sheetName, string cellStart,string cellEnd, string format="json")
{
var endpointUrl = BaseAddress + string.Format("/_vti_bin/ExcelRest.aspx/{0}/Model/Ranges('{1}!{2}|{3}')?$format={4}", fileUrl,sheetName,cellStart,cellEnd,format);
return DownloadString(endpointUrl);
}
}
和 SharePointOnlineCredentials
class 通过用户凭据访问 SharePoint Online 资源
static ICredentials GetCredentials(string userName, string password)
{
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}
return new SharePointOnlineCredentials(userName, securePassword);
}
结果
参考资料