Opening a pre-existing excel file

10,625

Found a solution that worked for me :

     var mySheet = Path.Combine(Directory.GetCurrentDirectory(), "Sample.xlsx");
          Excel.Application xlApp = new Excel.Application();
          xlApp.Visible = true;


          if (xlApp == null)
          {
              MessageBox.Show("Excel is not properly installed!!");
              return;
          }

          try
          {
              Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(mySheet);

          }
          catch(Exception ex)
          {
              xlApp.Quit();

          }
Share:
10,625
Daniel Taki
Author by

Daniel Taki

Yogi, Electrical Engineer pursuing knowledge for the sake of knowledge. Let me know if I can help but most likely I'm the one who will need it!

Updated on June 04, 2022

Comments

  • Daniel Taki
    Daniel Taki over 1 year

    I am trying to open a pre-existing excel file from a directory on my computer. However, I am getting a COMexception when my code gets to the Workbooks.Open method. I'm am unsure what I'm dong wrong. Any help is appreciated.

    using Microsoft.Office.Interop.Excel;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Xml;
    
    namespace Excel_Create
    {
        class Program
        {
    
            static void Main(string[] args)
            {
    
    
                string mySheet = @"‪‪‪‪C:\Users\Danny\Documents\Visual Studio 2013\Projects\MWS\MWS\bin\Debug\csharp-Excel.xls";
                  Excel.Application xlApp = new Excel.Application();
                  xlApp.Visible = true;
    
    
                  if (xlApp == null)
                  {
                      MessageBox.Show("Excel is not properly installed!!");
                      return;
                  }
    
    
    
             Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(mySheet,
              0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
              true, false, 0, true, false, false              );
    
            }
        }
    }
    

    Here is what the exception says: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Excel_Create.exe

    Additional information: '‪‪‪‪C:\Users\Danny\Documents\Visual Studio 2013\Projects\MWS\MWS\bin\Debug\csharp-Excel.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.

    • adv12
      adv12 over 8 years
      If the file is there, is it open in another application (e.g. Excel)?
    • Daniel Taki
      Daniel Taki over 8 years
      Yep it's there. It's a Microsoft Excel 97-2003 Worksheet (.xls) saved in : C:\Users\Danny\Documents\Visual Studio 2013\Projects\MWS\MWS\bin\Debug\csharp-Excel.xls Copy/paste from properties view of the file.
  • Daniel Taki
    Daniel Taki over 8 years
    Nope didn't work copy and pasted your code over mine and ran - still the same error
  • David
    David over 8 years
    @DanielTaki are you sure about the filename extension? Newer versions of Excel use .xlsx instead of .xls. Are you sure that it's .xls?
  • Daniel Taki
    Daniel Taki over 8 years
    No I'm not sure the excel is from Microsoft office 2010. Should I try and save the file as a .xlsx then and try to run program again?
  • Daniel Taki
    Daniel Taki over 8 years
    I don't know if this is pertinent information but I may have a cracked copy excel