tags:

views:

169

answers:

0

After some google searching I found out how to read excel column names How To Retrieve Schema Information by Using GetOleDbSchemaTable and Visual Basic .NET . Though the sample is in VB.Net, I am using C#.net. My code is :

public DataSet ReadXslToDataset(string fileName,string sheetName)
            {
                ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+fileName+";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
                using (objConn = new OleDbConnection(ConnectionString))
                {
                        objConn.Open();

                        String[] restrection = { null, null, sheetName, null };
                        dtColumnNames = objConn.GetOleDbSchemaTable  (OleDbSchemaGuid.Columns, restrection);
                        string strColumnName = string.Empty;
                        if (dtColumnNames != null)

                          strColumnName = dtColumnNames.Rows[0]["COLUMN_NAME"].ToString();

                }
}

But I am finding that the column field is blank and henceforth getting the exception

There is no row at position 0.

The excel file looks as

S.No    Issue
1      log4net message pattern not displayed properly in the log file
2      Reading blank rows from Excel while populating into dataset

and I have ensured that I am passing the right file & sheet name.