尝试从中读取数据*.XLS 文件。我能够读取所有单元格字符串,但是无法读取数字在细胞中。我尝试使用 .ToString() 方法但没有成功。取回数据库null.
Code:
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();
}
}
cleanString.AppendFormat("行[{0}],数据类型[{1}],字符串[{2}],
Column[{3}] |-|", 计数器, (reader[i]).GetType(),
reader[i].ToString(), i);
生产:
行[98],数据类型[System.DBNull],字符串[],列[4] |-|
Column 4包含混合字符串和数字,例如:1300、2341、5000、4000(国内长途)、1243
尝试使用“GetInt、GetValue、Parse、‘casting’、ToString() 等”,但 null 就是 null!有人想引导我走向正确的方向吗?
示例 XLS 文件:
https://github.com/abflett/adamflett_ca https://github.com/abflett/adamflett_ca
https://github.com/abflett/adamflett_ca/blob/master/sample.xls https://github.com/abflett/adamflett_ca/blob/master/sample.xls