Click here to Skip to main content
15,123,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to be able to Open an excel file that is in web using a C# desktop application without having to build a web application of sort. (working on a small budget )

I have some code that works where I can open and edit a local excel xlsx ( on the C drive for example ).

However I want to be able to open and read/edit a xlsx file that is in web ( say one drive )

code below works for local file.
any help is appreciated.

What I have tried:

Microsoft.Office.Interop.Excel.Application XlApp = null;
            Microsoft.Office.Interop.Excel.Workbooks XlBooks = null;
            Microsoft.Office.Interop.Excel.Workbook XlBook = null;
            Microsoft.Office.Interop.Excel.Range XlRange = null;
           

            Microsoft.Office.Interop.Excel.Worksheet XlSheet = null;
            int iRowCout = 0; int iColCount = 0;


            // string sXLFilefillName = @"C:\Temp\Test.xlsx";
            string sXLFilefillName = @"C:\Users\Ad\OneDrive\Test_OneDrive.xlsx";
            
            
            XlApp = new Microsoft.Office.Interop.Excel.Application();
            XlBooks = XlApp.Workbooks;
            XlBook = XlBooks.Open(sXLFilefillName);

            XlSheet = XlBook.Worksheets["Sheet1"];  //-- see video 19:05      

            XlRange = XlSheet.UsedRange;
            iRowCout = XlRange.Rows.Count;  // see first video
            iColCount = XlRange.Columns.Count;  // see second vidoe 7:30

            //--- output
            txtOut.Text = txtOut.Text + " --- Output --- ";
            txtOut.Text = txtOut.Text + Environment.NewLine + "Rowcount: " + iRowCout.ToString();
            txtOut.Text = txtOut.Text + Environment.NewLine + "Col count: " + iColCount.ToString();

            string sVal = "";

            XlRange.Cells[5, 1] = "Five";
            XlRange.Cells[20, 1] = "twenty";

            XlBook.Save();

            sVal = (XlRange.Cells[1, 1] as Microsoft.Office.Interop.Excel.Range).Value2 as string;
            txtOut.Text = txtOut.Text + Environment.NewLine + "after update cell 1A : " + sVal.ToString();

            //--closing 
            XlBook.Close();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(XlBook);
            XlBook = null;
            GC.Collect();

            XlBooks.Close();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(XlBooks);
            XlBooks = null;
            GC.Collect();

            XlApp.Quit();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(XlApp);
            XlApp = null;
            GC.Collect();
Posted
Updated 22-Sep-21 22:49pm

1 solution

Most websites don't allow you to modify files stored on them. You would need to download the file locally, edit it, and then find some way to upload your changes back to the site.

For OneDrive, you might be able to use the API:
OneDrive file storage API overview - Microsoft Graph | Microsoft Docs[^]

But if it's in the user's own OneDrive, then they probably have a local copy which will be synced to the web. You would just need to open and edit that local copy.
   
Comments
idkd 23-Sep-21 5:30am
   
Thank you Richard for providing the API link. Guess with the small budget my customer may not like that approach. However its good to know.

I was trying to get the file update to work on the OneDrive folder - however at the moment having trouble with synching - could be a issue with my account I am trying with. Also do you know if there is anyway that you could get the OneDrive files to synch real time ?
*24/09/21 - just updating - got onedrive file to update near-realtime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900