public DataTable GetCsvData(string filePath, string fileName = "Shipping")
{
string path = Path.Combine(filePath, fileName + ".csv");
string connString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + filePath + ";Extensions=asc,csv,tab,txt;";
try
{
using (OdbcConnection odbcConn = new OdbcConnection(connString))
{
odbcConn.Open();
OdbcCommand oleComm = new OdbcCommand();
oleComm.Connection = odbcConn;
oleComm.CommandText = "select * from [" + fileName + "#csv]";
OdbcDataAdapter adapter = new OdbcDataAdapter(oleComm);
DataSet ds = new DataSet();
adapter.Fill(ds, fileName);
return ds.Tables[0];
odbcConn.Close();
}
if (File.Exists(path))
{
File.Delete(path);
}
}
catch (Exception ex)
{
if (File.Exists(path))
{
File.Delete(path);
}
throw ex;
}
}
但是会发现如果源cvs中的列名存在不同的字符:{}/()/[] 会被转换为() 所以会出现无法匹配的问题。