【发布时间】:2013-08-29 12:53:03
【问题描述】:
我正在使用 OLEDB Data Provider 来读取 excel 文件,但问题是在 excel 表中某些 cloumn 具有无效值,例如,而不是数字字符串, 当我读取这个无效值时,我得到一个空字符串而不是实际值。
对于上面的截图,当我读取值 john 得到空字符串时。
那么有什么方法可以读取这个无效值吗?
任何帮助将不胜感激。
代码是读取excel文件
private DataTable ReadExcelFile(string sheetName, string path)
{
using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
string Import_FileName = path;
string fileExtension = Path.GetExtension(Import_FileName);
if (fileExtension == ".xls")
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 8.0;HDR=YES;'";
if (fileExtension == ".xlsx")
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;'";
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "Select * from [" + sheetName + "$]";
comm.Connection = conn;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
return dt;
}
}
}
}
【问题讨论】: