以编程方式修改 Sharepoint Online Excel 文件
Programmatically modifying a Sharepoint Online Excel file
我们需要创建一个 c# 应用程序来修改我们的一个 Sharepoint 在线站点中的 Excel 文件。
对于本地文件,我是这样的:
Excel.Application excel_app = new Excel.Application();
excel_app.Visible = true;
Excel.Workbook workbook = excel_app.Workbooks.Open(
<path to excel file>,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
但是,如果我将 Sharepoint Excel 文件的 url 放入其中,它就不起作用了。这是否可行,如何实现?
您可以下载文件,修改,上传文件,替换
这是连接到共享点网站并上传文件的方法
使用 Microsoft.SharePoint.Client 命名空间
using SP = Microsoft.SharePoint.Client;
...
using (var context = new SP.ClientContext(new Uri(<YOURSITEURL>))) {
var web = context.Web;
context.Credentials = new NetworkCredential(<NETWORK_USERNAME>, <NETWORK_PASS>, <DOMAIN_NAME>);
context.Load(web);
try
{
context.ExecuteQuery();
} catch (Exception ex) {
}
var file = web.GetFileByServerRelativeUrl(new Uri(<FILE_URL>).AbsolutePath);
context.Load(file);
try
{
context.ExecuteQuery();
file.SaveBinary(new SP.FileSaveBinaryInformation() { Content = Encoding.UTF8.GetBytes(<NEW_FILE>) });
try
{
context.ExecuteQuery();
}
catch (Exception ex)
{
}
}
}
上传一个文件(excel,word等)到Sharepoint online下面是代码:
注意:在 .net FRAMEWORK 中创建项目(不要在 .net core 中这样做)并安装 Microsoft.SharePointOnline.CSOM
的 nuget 包
ClientContext ccontext = new ClientContext("<URL>/sites/<foldername>");
var securePassword = GetPasswordAsSecureString("<YOUR PASSWORD>"); // function is below
ccontext.AuthenticationMode = ClientAuthenticationMode.Default;
var onlineCredentials = new SharePointOnlineCredentials("<YOU-EMAIL-ID>", securePassword);
ccontext.Credentials = onlineCredentials;
List lst = ccontext.Web.Lists.GetByTitle("Documents");
var DestSubFolder = "General/Data";
Folder fileFolder = lst.RootFolder;
if (DestSubFolder.Length > 0)
fileFolder = CreateFolderInternal(ccontext.Web, lst.RootFolder, DestSubFolder);
// upload File.
FileCreationInformation newFile = new FileCreationInformation();
byte[] FileContent = System.IO.File.ReadAllBytes("c://Temp/test.txt");
newFile.ContentStream = new MemoryStream(FileContent);
newFile.Url = Path.GetFileName("c://Temp/test.txt");
newFile.Overwrite = true;
fileFolder.Files.Add(newFile);
fileFolder.Update();
ccontext.ExecuteQuery();
private static SecureString GetPasswordAsSecureString(string password)
{
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
return securePassword;
}
我们需要创建一个 c# 应用程序来修改我们的一个 Sharepoint 在线站点中的 Excel 文件。
对于本地文件,我是这样的:
Excel.Application excel_app = new Excel.Application();
excel_app.Visible = true;
Excel.Workbook workbook = excel_app.Workbooks.Open(
<path to excel file>,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
但是,如果我将 Sharepoint Excel 文件的 url 放入其中,它就不起作用了。这是否可行,如何实现?
您可以下载文件,修改,上传文件,替换
这是连接到共享点网站并上传文件的方法
使用 Microsoft.SharePoint.Client 命名空间
using SP = Microsoft.SharePoint.Client;
...
using (var context = new SP.ClientContext(new Uri(<YOURSITEURL>))) {
var web = context.Web;
context.Credentials = new NetworkCredential(<NETWORK_USERNAME>, <NETWORK_PASS>, <DOMAIN_NAME>);
context.Load(web);
try
{
context.ExecuteQuery();
} catch (Exception ex) {
}
var file = web.GetFileByServerRelativeUrl(new Uri(<FILE_URL>).AbsolutePath);
context.Load(file);
try
{
context.ExecuteQuery();
file.SaveBinary(new SP.FileSaveBinaryInformation() { Content = Encoding.UTF8.GetBytes(<NEW_FILE>) });
try
{
context.ExecuteQuery();
}
catch (Exception ex)
{
}
}
}
上传一个文件(excel,word等)到Sharepoint online下面是代码: 注意:在 .net FRAMEWORK 中创建项目(不要在 .net core 中这样做)并安装 Microsoft.SharePointOnline.CSOM
的 nuget 包 ClientContext ccontext = new ClientContext("<URL>/sites/<foldername>");
var securePassword = GetPasswordAsSecureString("<YOUR PASSWORD>"); // function is below
ccontext.AuthenticationMode = ClientAuthenticationMode.Default;
var onlineCredentials = new SharePointOnlineCredentials("<YOU-EMAIL-ID>", securePassword);
ccontext.Credentials = onlineCredentials;
List lst = ccontext.Web.Lists.GetByTitle("Documents");
var DestSubFolder = "General/Data";
Folder fileFolder = lst.RootFolder;
if (DestSubFolder.Length > 0)
fileFolder = CreateFolderInternal(ccontext.Web, lst.RootFolder, DestSubFolder);
// upload File.
FileCreationInformation newFile = new FileCreationInformation();
byte[] FileContent = System.IO.File.ReadAllBytes("c://Temp/test.txt");
newFile.ContentStream = new MemoryStream(FileContent);
newFile.Url = Path.GetFileName("c://Temp/test.txt");
newFile.Overwrite = true;
fileFolder.Files.Add(newFile);
fileFolder.Update();
ccontext.ExecuteQuery();
private static SecureString GetPasswordAsSecureString(string password)
{
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
return securePassword;
}