Thursday, March 12, 2009

The bug of using Office Excel COM component in .NET

Thinking of producing a parser to read the information from Excel Document, I chose the Microsoft .NET developing environment and using the Micorosft Office COM component provided by Microsoft Office suite. 

When I started the first step of the developing by using the COM component to launch the Excel program and read the basic info. It sucks! The COM component throw an COM exception(Com Exception 0x80028018 TYPE_E_INVDATAREAD) and I cannot work out what is wrong. 

So later I used Google to search and find the solution on CodeProject.com, it's about the culture settings of the current .NET program thread.

If you meet the same problem, you can fix it like this:
Before launching Excel
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
After closing Excel
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;