Excel integration in (ASP).NET using OleDb

Most of my private programming time lately has been invested in my PhotoAlbum's new version, in which I implement bug corrections and prio 1 improvements over the version I used during the last holidays in Asia. I will publish that version soon together with a list of improvements.
One important feature (or so I thought) is the ability to import the content of an Excel file into the XML files I use in the PhotoAlbum system. I use an Excel file as main repository for all my pictures' information, not a database. The main reason is that I want to be able to edit the pictures info when I am on the road, using my Palm LifeDrive together with DocumentsToGo. So the idea was: I will input the pictures info only once in the Excel file, post the file through FTP to the web server, and then import the new pics' information into the PhotoAlbum.
Well, the import works great (I use ADO.NET to connect to the Excel file, use a few SQL statements to populate my objects and then save all to XML). Tested on the production server, it works too, very happy. However, when I tried to enter data into the real Excel file on my PDA today, I got the dreaded error 6150 "Cannot save in native Excel format"... This error happens from time to time, and I have no idea why. Asking Dataviz support didn't help (though they did their best to try and answer me), so... I think that I need a more solid, though less comfortable format...
Bottom line is, the "Import from Excel" functionality will be very useful to me when I am home (or near a PC). However I cannot rely on it when I am on the PDA only, so I need to use another format, probably CSV. The advantage of CSV is that I can easily produce it on the PDA, and can import it into Excel easily too. The other alternative is to write a XML to Excel converter for the PhotoAlbum files. This way, I'll enter the information in XML when on the road, and when I am home I just import the whole thing into my main Excel file. That would also eliminate the need for a 3rd format (XML, Excel, CSV). I need to think about it.
There are not so many examples online about how to connect to an Excel file using ADO.NET 2.0, so I post a (simplified) example here.
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
  + "Data Source=" + strPathExcel + ";"
  + "Extended Properties=Excel 8.0;";

// Create connection object by using the preceding connection string.
OleDbConnection cnConnection = new OleDbConnection( strConnection );

// Open connection with the database.
cnConnection.Open();

try
{
  // Get a reader for the ImageCollections.
  // For example's sake, this imports values from the
  // worksheet named "MyData". Note the trailing '$'
  OleDbCommand cmdSelectSections
    = new OleDbCommand( "SELECT * FROM [MyData$]", cnConnection );

  OleDbDataReader drdSections =
    cmdSelectSections.ExecuteReader();

  // Import excel rows into XML files
  while ( drdSections.Read() )
  {
    // The columns can be accessed by index, or by using
    // the Title string, which is the content of the
    // corresponding column on the first row.

    // For example:
    string strPath = (string) drdSections[ "Path" ];

    // Imported values should be tested against DbNull.Value
    // and also casted to the corresponding type. For example
    // numbers are imported as double, even if they are integers.

    // More code...
  }
}
catch ( Exception )
{
  throw;
}
finally
{
  // Make sure that connection is properly closed.
  cnConnection.Close();
}
Connecting to an Excel file using ADO.NET 2.0
Print | posted on Friday, August 25, 2006 2:23 PM

Feedback

# re: Excel integration in (ASP).NET using OleDb

left by Wiebe Tijsma at 12/15/2006 1:22 PM Gravatar
Hi Laurent,

Just some notes, it might be useful to include "Extended Properties=Excel 8.0;IMEX=1" when connecting to excel, in case of mixed-mode columns (string and numeric for example) (in the first 8 rows) this will convert the values to the string type instead of outputting NULL values.

Just a comment on the C#:

throw ex;

is a bad practice, as it throws a new exception, losing the current stack trace. use:

throw;

instead to preserve it :)

Wiebe Tijsma


# re: Excel integration in (ASP).NET using OleDb

left by Wiebe Tijsma at 12/15/2006 1:50 PM Gravatar
Ah additionally this requires you to make some changes to the registry:

Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows
Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedTypes

TypeGuessRows: setting the value to 0 (zero) will force ADO to scan
all column values before choosing the appropriate data type.

ImportMixedTypes: should be set to value 'Text' i.e. import mixed-type
columns as text:

Using IMEX=1 in the connection string ensures the
registry setting is applied.

# re: Excel integration in (ASP).NET using OleDb

left by Laurent at 12/15/2006 2:32 PM Gravatar
About the "throw ex;": That's correct. I even blogged about it here:
http://geekswithblogs.net/lbugnion/archive/2006/09/29/92708.aspx

About the mixed columns: In my use cases, it was never needed. Having mixed types in a columns is not a good idea in my opinion. Having to change the registry to use it is not applicable in many cases, for example when you run an ASP.NET application (my case), and have only limited access to the server's settings. I prefer to stick to the one column = one type paradigm.

Thanks,
Laurent

# re: Excel integration in (ASP).NET using OleDb

left by Laksh at 3/20/2007 9:50 PM Gravatar
thanks..this post really helped me a lot. But i dont agree with Laurent that "Having mixed types in a columns is not a good idea". Becuase if data is comming from client then we dont have control over how data is comming to us. Second thing, most of the time IDs are alphanumeric characters like DL, Insuarnce ID, Group ID etc etc I dont understand why cant OLE db just read the values and let the user decide what to do with invalid values.

# re: Excel integration in (ASP).NET using OleDb

left by Scott Mather at 4/4/2007 12:06 AM Gravatar
Wiebe, you're a lifesaver! IMEX=1 just eliminated my headache.

# re: Excel integration in (ASP).NET using OleDb

left by eXeCuToR at 5/31/2007 6:42 AM Gravatar
Wiebe, about exceptions...
Actually it's just ridiculous to catch an exception then throw it again.
And didn't mention the performance yet...

# re: Excel integration in (ASP).NET using OleDb

left by Laurent at 5/31/2007 7:05 AM Gravatar
@ eXeCuToR:

No, no, it makes sense sometimes to catch an exception and rethrow it: Imagine that your class opens a DB connection. Imagine that something happens during the treatment, and an exception is thrown. You need to close the DB connection! So you catch the exception, close the connection (typically in the "finally" clause), then you pass the exception to the upper level, for example to display a message to the user.

That said, see this:
http://geekswithblogs.net/lbugnion/archive/2006/09/29/92708.aspx

HTH,
Laurent

# re: Excel integration in (ASP).NET using OleDb

left by Manu at 6/7/2007 4:39 AM Gravatar
Hi,

I am using the same method to connect to excel. It works perfect on my machine but doesnt work on the server. Now excel is not installed on the server nor is the OLEDB data provider.

Which of these is a necessary prerequisite for the application to work on the server?

If you let me know the answer I would sincerely thankful to you as it would save me a lot of time and effort.

Thanks in advance.
Manu

# re: Excel integration in (ASP).NET using OleDb

left by Nilesh at 12/4/2007 6:47 PM Gravatar
Hi,

if i do not specify HDR=Yes;IMEX=1 then my connection works fine but as i includes these extra parameter to read mixed data then i am getting error as "Could not find installable ISAM. ".
I have tried to resolve issus by registering dll file but its not working .. could any one help please..................

Thanks,
Nilesh

# re: Excel integration in (ASP).NET using OleDb

left by Rahul at 3/3/2008 6:43 PM Gravatar
Its working fine for excel2003. bt nt fr 2007.
Could you please help me..I would be very much thanqful to u.

# re: Excel integration in (ASP).NET using OleDb

left by Sondra Bertsch at 3/13/2008 1:05 AM Gravatar
Never have mixed content in a cell? Would you tell the auto industry that their part numbers should either be all numeric or all alpha-numeric because I have a table with millions of rows with mixed data for part numbers. Live in the real world.

# re: Excel integration in (ASP).NET using OleDb

left by Laurent at 3/13/2008 1:15 AM Gravatar
Hi,

Not sure where the aggressive tone comes from :) Let's review what I wrote:

"About the mixed columns: In my use cases, it was never needed. Having mixed types in a columns is not a good idea in my opinion. Having to change the registry to use it is not applicable in many cases, for example when you run an ASP.NET application (my case), and have only limited access to the server's settings. I prefer to stick to the one column = one type paradigm."

Notice how I use the words "my use cases", "in my opinion", "not applicable in many cases", "I prefer". Yes, it's true that in MY case, I never had to use mixed content cells. And yes, I would recommend against it. That said, you will always find a special case where you need a special configuration, and I am more than ready to acknowledge that it will cause issues (that you need to modify the registry to make it work should hint that it's not really a default case...)

Then again, a table with millions rows in Excel? Really? I am not even sure that's possible, and even if it was, in such a case, you should really consider using a database!!

Happy to discuss this further if you want, and BTW last time I checked, this was indeed the real world, a world where experts exchange information respectfully :)

# re: Excel integration in (ASP).NET using OleDb

left by Kamil at 3/28/2008 8:06 AM Gravatar
"Could not find installable ISAM. " reg looks fine, dll's are in place and it still isn't working... any help would be appreciated.

Thank you
Kam

# re: Excel integration in (ASP).NET using OleDb

left by satalaj at 4/24/2008 8:24 AM Gravatar
Thanks,
Regedit resolved the excel formating problem

Satalaj

# re: Excel integration in (ASP).NET using OleDb

left by nitin at 6/18/2008 11:47 AM Gravatar
i used this it is throwing error for ISAM

# re: Excel integration in (ASP).NET using OleDb

left by James Knowlton at 7/14/2008 9:38 PM Gravatar
I use this method regularly for New database projects. I store all the Code table values in excel so I can reload them whenever I rebuild the database.

I have run into one problem though, I have a code table of Country Codes along with the associated flags for those countries. I can not find any method to access the "Pictures" on the sheet through the oledb connection? Any ideas? The pictures were copied from various web sites and pasted into the sheet in a specific column. The column always appears to be empth when reading it through OLEDB so I was wondering if you knew how to access a picture associated with a specific Cell?

Thanks,

James.

# re: Excel integration in (ASP).NET using OleDb

left by Satyendra at 8/2/2008 5:46 AM Gravatar
Hi Nilesh,
In case of error " ISAM no found " you should write the extendedn properties like
Extended Properties="+ (char)34 + "Excel 8.0;HDR=Yes;IMEX=1"+ (char)34;
Now ISAM related error would not come.
thanks
Satyendra
Title  
Name
Email (never displayed)
Url
Comments   
Please add 6 and 2 and type the answer here: