开发者

Issue while reading excel column names by using GetOleDbSchemaTable

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.


There is no row at position 0.

Try

if (dtColumnNames != null) {
   if(dtColumnNames.Rows.Count > 0){
      strColumnName = ColumnNames.Rows[0]["COLUMN_NAME"].ToString
    }
  }

Sorry I may have the syntax slightly out - I'm more of a VB.net guy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜