试图从*.XLS文件读取数据。我能够读取所有单元格字符串,但无法读取单元格中的数字。我尝试过使用.ToString()方法,但没有成功。获取一个数据库空。
代码:
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(“行{0},DataType{1},字符串{2},列{3}{2}”),计数器,(readeri).GetType(),readeri.ToString(),i;
生产:
Row98,DataTypeSystem.DBNull,String[],Column4连体
列4包含混合字符串和数字,例如: 1300、2341、5000、4000 (DED)、1243
尝试使用“GetInt,GetValue,Parse,‘强制转换’,ToString()等”,但是null是空的!有人想把我引向正确的方向吗?
示例XLS文件:
发布于 2016-04-20 01:50:51
花了些功夫,但结果却是个简单的解决办法。
连接字符串是问题所在。
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上找到的视频:Hz54BU
并且已经回答了这里的堆栈溢出:Help with a OleDB connection string for excel files
https://stackoverflow.com/questions/36723035
复制相似问题