March 3, 2009

Ignoring Header in Excel using .NET

I recently was put on a project that requires an Excel file to be uploaded. It must then be read and data in it, validated. The solution I chose was to use a OleDbDataReader. However, the Excel file can come in with or without headers. What I mean is that the very first row may or may not contain headers.

After researching the web for a while, my colleagues and I found the following property can be set in the connection string. Here's the code:

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + [Your Excel File Name Here] + ";Extended Properties=Excel 8.0;HDR=No";

Setting HDR=No will ignore the first row as header.