Click here to Skip to main content
15,891,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried to populate a combobox to fill up with the worksheets that are available in the workbook uploaded. With excel files with the format .xls, it works just fine but whenever i tried files with the format .xlsx. It prompts an error

Error:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'


I am not quite sure why, I have tried to search up the error to understand it further but to no luck.

What I have tried:

This is my code:

public void cmbSheets(string cmb, string path)
{
   Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();

   if (cmb == "Kit")
   {
     Workbook workBook = application.Workbooks.Open(path);

     string workbookName = workBook.Name;
     int numberOfSheets = workBook.Sheets.Count;

     List<string> workSheetNames = new List<string>();
     for (int i = 1; i <= workBook.Sheets.Count; i++)
     {
       Worksheet worksheet = workBook.Worksheets[i];
       workSheetNames.Add(worksheet.Name);
     }

     cmbKit.DataSource = workSheetNames;

     workBook.Close(false, path, null);
     Marshal.ReleaseComObject(workBook);
   }
}
Posted
Updated 20-Jul-22 2:07am
Comments
Pete O'Hanlon 10-Jun-22 6:46am    
Put a breakpoint at the start of your code and step through it line by line to see which one throws the exception. I have a suspicion you're going to hit it on ReleaseComObject but you need to verify this. If you are just trying to close your application there, you just need to call application.Quit(); rather than ReleaseComObject.
Allysha April 12-Jun-22 21:08pm    
It stops at the line "Workbook workBook = application.Workbooks.Open(path);"
Pete O'Hanlon 13-Jun-22 3:54am    
Okay, so the problem you have here is with having a version of Excel registered that cannot open .xslx format files.
Richard MacCutchan 10-Jun-22 6:58am    
Which version of Excel do you have installed?
ludosoep 10-Jun-22 15:07pm    
+1

1 solution

I'd suggest to use EPPlus[^].

C#
FileInfo fi = new FileInfo("FullFileNameHere");
using(ExcelPackage xlPackage = new ExcelPackage(fileInfo))
{
    List<string> sheets = xlPackage.Workbook.Worksheets.Select(x=>x.Name).ToList();
    foreach (string s in sheets)
        cmbKit.Items.Add(s);
}


As you can see, the code is very short. It executes very fast.

More about EPPlus software you'll find here: EPPlus 5[^]
 
Share this answer
 

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