我们需要创建一个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放在一边就行不通了。这是可行的,怎么样?
答案 0 :(得分:0)
您可以下载文件,修改文件,上传文件并替换它
以下是如何连接到sharepoint站点并上传文件
使用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)
{
}
}
}
答案 1 :(得分:0)
以下是将文件(excel,word等)上传到在线共享点的代码: 注意:在.net FRAMEWORK中创建项目(不要在.net核心中执行此操作),然后安装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;
}