none
How to open Excel with external data connection and get up-date data in C# program? RRS feed

  • Question

  • I have an Excel file which contains an web query to website. This web query updates every 1 minutes.

    I hope to open this Excel file in my C# program and get data from it. How can I get the up-dated data from file?

    Thanks.

    Monday, June 27, 2016 1:03 PM

Answers

  • Hi kaiyuanl,

    you can try to use the code below to get the data from Excel file.

    static void Main(string[] args)
            {
                String file = @"C:\Users\v-padee\Desktop\Book1.xlsx";
            
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                xlApp.Visible = true;
    
                //Excel.Application excel = null;
                Excel.Workbook wkb = null;
               // excel = new Excel.Application();
    
                wkb = xlApp.Workbooks.Open(file);
    
    
                Excel.Worksheet sheet = wkb.Sheets["Sheet1"] as Excel.Worksheet;
    
                Excel.Range range = null;
    
                if (sheet != null)
                    range = sheet.get_Range("A1");
    
                string A1 = String.Empty;
    
                if (range != null)
                    A1 = range.Text.ToString();
    
                Console.WriteLine("A1 value: {0}", A1);
            }

    Note:-

    This is just a demo code to give you an example how you can achieve your requirement.

    you have to modify the code to get the desire output.

    also it is recommended to you that you try to use exception handling and after complete your work make the objects null and quit the excel application.

    Regards

    Deepak 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 28, 2016 5:08 AM
    Moderator