Working with Excel files using c#

During my current project I had some work of reading and writing from an Excel file.  I simply started with a Google Search :) and found many solutions, but the one provided by David Hayden was excellent Read Here.  Here I wont describe this article as its already really good, I will discuss the problem encountered in using this instead.

One major problem was to read the Excel spreadsheet at first place, David has used a connection string somewhat like:

string connectionString = @”Provider=Microsoft.Jet.
OLEDB.4.0;Data Source=Book1.xls;Extended
Properties=”"Excel 8.0;HDR=YES;”"”;

I was trying to read from an Office 2007 excel file and exceptions were encountered due to the above connection string. so I changed it to :

string connectionString = String.Format(@”Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=”"Excel 12.0;HDR=YES;”"”, this.FilePath);

where this.FilePath contains the full path of the excel file.

Changing the conn string worked like a charm and everything is working fine now.

Enhanced by Zemanta
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Twitter
  • TwitThis

Leave a Reply

Spam protection by WP Captcha-Free