Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while opening an excel workbook using C# code

Tags:

c#

excel-2007

I'm trying to open an excel workbook and trying to get a worksheet in it. Excelapp.workbooks.Open line is throwing an Exception as

System.Runtime.InteropServices.COMException from HRESULT: 0x800A03EC at Microsoft.Office.Interop.Excel.Workbooks.Open

Here is my code:

Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(strWBPath, 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, true);

StrWbPath is my Excel location. I'm refering to 2007 excel and added Microsoft.office.interop.excel of version 12.0.0.0.

like image 446
Amrita Avatar asked Nov 09 '09 09:11

Amrita


People also ask

How do I open an Excel spreadsheet in C?

while if you want to open an excel sheet, you click the C folder and type the name of your file in the search bar which is at the top right hand corner of the page. then just click on the file.

How do you fix the workbook Cannot be opened?

Repair a corrupted workbook manuallyOn the File tab, click Open. In Excel 2013 or Excel 2016, click on the location where the spreadsheet is located, and click Browse. In the Open dialog box, select the corrupted workbook that you want to open. Click the arrow next to the Open button, and then click Open and Repair.

Why is my Excel not opening?

Another possible reason for Excel not opening in Windows 10 is if Microsoft Office is corrupt. You could choose to uninstall it or update the package, but that's a lot of work. Instead, you should try the repair option first; it is ideal even for updated Microsoft Office options.


1 Answers

(Sorry for answering this old question, but it's google result #1 for this problem and the correct answer is missing).

The error occurs because excel thinks the Workbook has been corrupted. When opening the Excel file, the last parameter tells excel how to handle this situation. Pass Microsoft.Office.Interop.Excel.XlCorruptLoad.xlExtractData to instruct it to retrieve the data (This will open a popup telling the user excel tried to extract the data if the file is corrupted).

However, I noticed this problem can also be caused if the workbook you are trying to open has a different locale than your excel (was saved on a machine using another language setting) OR your system not having the en-us locale set.

While this is extremely stupid, it's easy to overcome. For me the solution was to just set the current locale to en-us before opening the file:

static System.Globalization.CultureInfo oldCI;
oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlExtractData);
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

credit goes to this page

like image 145
hotfire42 Avatar answered Oct 21 '22 01:10

hotfire42