OleDbDataReader 如何读取数字类型?
OleDbDataReader How to read number types?
正在尝试从 *.XLS 文件 中读取数据。我能够读取所有单元格字符串,但无法读取单元格中的数字。我尝试使用 .ToString() 方法但没有成功。取回数据库 null.
代码:
public xlsConverter(string excelFilePath, int numColumns)
{
string cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR=No;'", excelFilePath);
string queryStr = "SELECT * FROM [Sheet1$]";
using (OleDbConnection connection = new OleDbConnection(cnnStr))
{
OleDbCommand command = new OleDbCommand(queryStr, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
for(int i = 0; i <= numColumns; i++)
{
cleanedString.AppendFormat("{0}{1}", (reader[i].ToString()), "|");
}
cleanedString.AppendLine();
}
reader.Close();
}
}
cleanedString.AppendFormat("Row[{0}], DataType[{1}], String[{2}],
Column[{3}] |-|", counter, (reader[i]).GetType(),
reader[i].ToString(), i);
产生:
Row[98], DataType[System.DBNull], String[], Column[4] |-|
第 4 列 包含混合的字符串和数字,例如:1300、2341、5000、4000 (DED)、1243
尝试使用“GetInt、GetValue、Parse、‘casting’、ToString() 等”,但 null 是 null!有人想引导我朝着正确的方向前进吗?
示例 XLS 文件:
https://github.com/abflett/adamflett_ca
https://github.com/abflett/adamflett_ca/blob/master/sample.xls
进行了一些挖掘,但最终得到了一个简单的修复。
问题出在连接字符串上。
string cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0'", excelFilePath);
ImportMixedTypes=Text;TypeGuessRows=0
^已添加!
我在以下位置找到的 Youtube 视频:
https://www.youtube.com/watch?v=jhPp_Hz54BU
并且已经在 Whosebug 上回答了:
Help with a OleDB connection string for excel files
正在尝试从 *.XLS 文件 中读取数据。我能够读取所有单元格字符串,但无法读取单元格中的数字。我尝试使用 .ToString() 方法但没有成功。取回数据库 null.
代码:
public xlsConverter(string excelFilePath, int numColumns)
{
string cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR=No;'", excelFilePath);
string queryStr = "SELECT * FROM [Sheet1$]";
using (OleDbConnection connection = new OleDbConnection(cnnStr))
{
OleDbCommand command = new OleDbCommand(queryStr, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
for(int i = 0; i <= numColumns; i++)
{
cleanedString.AppendFormat("{0}{1}", (reader[i].ToString()), "|");
}
cleanedString.AppendLine();
}
reader.Close();
}
}
cleanedString.AppendFormat("Row[{0}], DataType[{1}], String[{2}], Column[{3}] |-|", counter, (reader[i]).GetType(), reader[i].ToString(), i);
产生:
Row[98], DataType[System.DBNull], String[], Column[4] |-|
第 4 列 包含混合的字符串和数字,例如:1300、2341、5000、4000 (DED)、1243
尝试使用“GetInt、GetValue、Parse、‘casting’、ToString() 等”,但 null 是 null!有人想引导我朝着正确的方向前进吗?
示例 XLS 文件:
https://github.com/abflett/adamflett_ca
https://github.com/abflett/adamflett_ca/blob/master/sample.xls
进行了一些挖掘,但最终得到了一个简单的修复。
问题出在连接字符串上。
string cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0'", excelFilePath);
ImportMixedTypes=Text;TypeGuessRows=0
^已添加!
我在以下位置找到的 Youtube 视频: https://www.youtube.com/watch?v=jhPp_Hz54BU
并且已经在 Whosebug 上回答了: Help with a OleDB connection string for excel files