简体   繁体   English

C#通过SharePoint客户端更新SharePoint中Excel文件中的链接

[英]c# update links in excel file in sharepoint through sharepoint client

I have a main excel file which has links to several other sub excel files in a sharepoint folder. 我有一个主要的excel文件,该文件具有指向sharepoint文件夹中其他几个子excel文件的链接。 Let's call the main excel as main.xlsx, and the sub excels as sub1.xlsx, sub2.xlsx. 让我们将主excel称为main.xlsx,将sub excel称为sub1.xlsx,sub2.xlsx。

The first row of each excel is same(header row). 每个excel的第一行都是相同的(标题行)。 The second row of main.xlsx references sub1.xlsx. main.xlsx的第二行引用sub1.xlsx。 The 3rd row of main.xlsx references sub2.xlsx, and so on. main.xlsx的第三行引用sub2.xlsx,依此类推。

The 1st cell of 2nd row in main.xlsx contains the value equal to the content of 1st cell of 2nd row in sub1.xlsx. main.xlsx中第二行的第一单元格包含的值等于sub1.xlsx中第二行的第一单元格的内容。 The 2nd cell of 2nd row in main.xlsx contains the value equal to the content of 2nd cell of 2nd row in sub1.xlsx, and so on. main.xlsx中第二行的第二个单元格包含的值等于sub1.xlsx中第二行的第二个单元格的内容,依此类推。 Similarly, the cells of 3rd row in main.xlsx are linked to 2nd row of sub2.xlsx. 同样,main.xlsx中第三行的单元格链接到sub2.xlsx第二行。

Please refer to the image to understand the linking between the excels. 请参考图片以了解各excel之间的链接。 擅长

These all files are in a sharepoint folder. 这些所有文件都位于一个sharepoint文件夹中。 My aim is to download the main.xlsx file to my local server. 我的目的是将main.xlsx文件下载到本地服务器。 The excels sub1, sub2 will be modified by respective users. excel的sub1,sub2将由各自的用户修改。 So at the time of download, the main excel should have updated content. 因此,在下载时,主要的excel应该具有更新的内容。 This is where I'm facing problem. 这就是我面临的问题。

I want to update the links in the main excel before it is downloaded. 我想先更新主要Excel中的链接,然后再下载。

Below is my code 下面是我的代码

var clientContext = new ClientContext(url)
ClientContext context = new ClientContext("https://siteurl");
context.Credentials = new NetworkCredential("userid", "password", "domain");
context.Load(context.Web);
List list = context.Web.Lists.GetByTitle("document library");
context.Load(list);
context.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = "<view/>";
ListItemCollection licoll = list.GetItems(query);
context.Load(licoll);
context.ExecuteQuery();
string filePath = @"D:\Docs\sp\";
foreach(ListItem li in licoll)
{
    Microsoft.SharePoint.Client.File file = li.File;
    if(file!=null && file.Name == "main.xlsx")
    {
        var fileRef = file.ServerRelativeUrl;
        FileInformation fileInfo = file.OpenBinaryDirect(context, fileRef.ToString());

        var fileName = Path.Combine(filePath,(string)file.Name);
        using (var fileStream = System.IO.File.Create(fileName))
        {                  
            fileInfo.Stream.CopyTo(fileStream);
        }    
    }
}

The code is working fine. 该代码工作正常。 Only problem is if someone had updated either sub1 or sub2 excels, the values in main excel are not updated. 唯一的问题是,如果有人更新了sub1或sub2 excel,则主excel中的值不会更新。

I cannot update the links in main excel after downloading as the original files are in sharepoint. 下载后,由于原始文件位于sharepoint中,因此我无法更新主要Excel中的链接。 So how can I update the links in the main excel before I download it to local machine? 那么,在将其下载到本地计算机之前,如何更新主要Excel中的链接?

Is it achievable through excelservices or REST API? 是否可以通过excelservices或REST API实现?

First, that query will add a lot of extra overhead, no need for you to pull all list items. 首先,该查询将增加很多额外的开销,不需要您提取所有列表项。

Second, you edit the file after you get the local filestream using Excel's API. 其次,使用Excel的API获取本地文件流后,您可以编辑文件。 Because you don't have the file until you download it. 因为在下载文件之前您没有文件。 If you do this server side, then you can do it 'before it's downloaded'. 如果您在服务器端进行操作,则可以在“下载前”进行操作。 But theirs really no need, download it in memory within your code like you have shown above (fileinfo.Stream). 但是他们的确不需要,如上所示(fileinfo.Stream),将其下载到代码中的内存中。 Then go ahead and create a new Excel workbook in memory (you will need a copy of Office Developer Tools to do this. 然后继续在内存中创建一个新的Excel工作簿(您将需要Office Developer Tools的副本才能执行此操作。

Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application()
Workbook wb = xls.Application.Workbooks.Open(fileInfo.Stream);

/* edit document here */

wb.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault); //saves your changes to the local drive location you specified
wb.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM