none
Cannot clear this Exception! RRS feed

  • Question

  • Hi,

    I am trying to read some data from a .XLS file using the code below. (Got from online)

    I have some time online to crack this to no avail.

    I made sure that the 'Micros of.Office.Interop.Excel' is installed.

    I have limited knowledge, so I am suck. Apparently this issue is very common with Visual Studio.

    I have followed advice from other users. I have also tried a different version of VS, but still no luck.

    Can you please guide me to the solution of this problem ?

    Regards

    Francesco C

    private void button3_Click(object sender,EventArgs e)
                {
              
                string pathOfExcelFile = "C:\\Users\\FC\\Desktop\\cobra.xls";
                Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                
    
                excelApp.DisplayAlerts=false; //Don't want Excel to display error messageboxes  
                Workbook workbook = excelApp.Workbooks.Open(pathOfExcelFile,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); //This opens the file
                Worksheet sheet = workbook.ActiveSheet; //Get the first sheet in the file
                Range bColumn = sheet.get_Range("A",null);
    
                List<string> dataItems = new List<string>();
    
                foreach(object o in bColumn)
                    {
                    Range row = o as Range;
                    string s = row.get_Value(null);
                    dataItems.Add(s);
                    Console.WriteLine(s);
                    }
    
                }


    Tuesday, June 16, 2020 8:10 PM

Answers

  • Hi, thank you to all, for your help.

    So I have, now, three different option to explore.

    This an AMAZING community.

    Regards

    Francesco C

    • Marked as answer by Francesco2017 Thursday, June 18, 2020 5:39 PM
    Thursday, June 18, 2020 5:39 PM

All replies

  • Either you do not have Excel installed, or you are building a 64-bit app and your copy of Excel is 32-bits.  Assuming you have Excel installed, try setting the CPU to Win32.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, June 17, 2020 3:45 AM
  • Hi Tim.

    thank you for your replay.

    Yes I do not have Excel installed on my computer.

    Strange tough, in none of my searches this was ever mentioned.

    I thought that it was not necessary because of the use of .dll files on my PC.

    Regards

    Francesco C

    Wednesday, June 17, 2020 4:25 PM
  • you have this line in your source code:
    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    and you are surprised that is trying to call the application named Excel?

    And what did you think this comment would mean?
    //Don't want Excel to display error messageboxes  


    Wednesday, June 17, 2020 4:43 PM
  • Hi,

    yes you are right.

    But, if you are not a programmer( I am an engineer) it is not obvious.

    So is there another way of reading data from an .xls file?

    regards

    Francesco C

    Wednesday, June 17, 2020 4:51 PM

  • So is there another way of reading data from an .xls file?


    There are other ways like

    Microsoft OLE DB Provider for Jet

    or

    StgOpenStorage

     
    Wednesday, June 17, 2020 5:05 PM
  • The open source NPOI package can read and write Excel files.  It original came from Apache.

    https://github.com/dotnetcore/NPOI


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, June 18, 2020 6:41 AM
  • Another option for reading/writing Excel spreadsheets without having Excel installed is the open source SpreadsheetLight for .Net Framework
    Thursday, June 18, 2020 10:45 AM
  • Another option for reading/writing Excel spreadsheets without having Excel installed is the open source SpreadsheetLight for .Net Framework
    SpreadSheetLight only supports .xlsx

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, June 18, 2020 12:30 PM
    Moderator
  • Hi, thank you to all, for your help.

    So I have, now, three different option to explore.

    This an AMAZING community.

    Regards

    Francesco C

    • Marked as answer by Francesco2017 Thursday, June 18, 2020 5:39 PM
    Thursday, June 18, 2020 5:39 PM