简体   繁体   中英

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. Let's call the main excel as main.xlsx, and the sub excels as sub1.xlsx, sub2.xlsx.

The first row of each excel is same(header row). The second row of main.xlsx references sub1.xlsx. The 3rd row of main.xlsx references sub2.xlsx, and so on.

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. 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. Similarly, the cells of 3rd row in main.xlsx are linked to 2nd row of sub2.xlsx.

Please refer to the image to understand the linking between the excels. 擅长

These all files are in a sharepoint folder. My aim is to download the main.xlsx file to my local server. The excels sub1, sub2 will be modified by respective users. So at the time of download, the main excel should have updated content. This is where I'm facing problem.

I want to update the links in the main excel before it is downloaded.

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.

I cannot update the links in main excel after downloading as the original files are in sharepoint. So how can I update the links in the main excel before I download it to local machine?

Is it achievable through excelservices or 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. 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). Then go ahead and create a new Excel workbook in memory (you will need a copy of Office Developer Tools to do this.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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