Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用OleDbDataReader读取Excel-无法从特定列读取值_C#_Excel_Oledbdatareader - Fatal编程技术网

C# 使用OleDbDataReader读取Excel-无法从特定列读取值

C# 使用OleDbDataReader读取Excel-无法从特定列读取值,c#,excel,oledbdatareader,C#,Excel,Oledbdatareader,我正在使用OleDbDataReader处理现有的C#代码读取Excel文件。但我无法在两个特定列中获得单元格的内容 这是连接代码: connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathExcel + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";"); connection.Op

我正在使用OleDbDataReader处理现有的C#代码读取Excel文件。但我无法在两个特定列中获得单元格的内容

这是连接代码:

connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
                + pathExcel + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";");
connection.Open();
和访问默认工作表的内容:

tables = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
sheetsNames = from DataRow row in tables.Rows select row["TABLE_NAME"].ToString();
sql = "SELECT * FROM [" + sheetsNames.FirstOrDefault() + "];";
ocmd = new OleDbCommand(sql, connection);
reader = ocmd.ExecuteReader(); //OleDbDataReader
因此,最后我阅读了所有内容,但对于某些列,我无法访问cells内容(reader[“mycolumn”])。所以,我试过这个:

while (reader.Read()){
   // Test code, I tried different ways to read cell content
   // It's working
   string colName = reader.GetName(26);
   string val1 = reader[colName].ToString();
   string val2 = reader.GetValue(26).ToString();

   // Same code, changing index 26 to 27
   ... // always empty values. Bug ??
}
如果我对表达式“reader.GetValue(26)”求值,它将返回预期的值,但当它是“reader.GetValue(27)”时,它将返回一个异常(“此表达式会产生副作用,并且不会被求值”),特别是它类似于索引超出范围的异常。但我可以从下一列(29、30…)读取数据


你知道原因吗?

列中的前几个单元格是否抛出空异常?我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。尝试检查。不。第一个单元格包含列标题,其他单元格为空或包含“X”。@Rififi-可能是或的副本。请尝试“TypeGuessRows=0;ImportMixedTypes=Text”。