/// <summary>
/// 讀取csv文件
/// </summary>
/// <param name="filepath"></param>
/// <param name="filename"></param>
/// <returns></returns>
public DataTable ReadCVS(string filepath, string filename)
{
//string cvsDir = filepath;//要讀取的CVS路徑
DataTable dt = new DataTable();
if (filename.Trim().ToUpper().EndsWith("CSV"))//判斷所要讀取的擴展名
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='text;ColNameHeader=False;HDR=YES;FMT=Delimited'";//有列的讀取
string commandText = "select * from [" + filename + "]";//SQL語句
try
{
using (OleDbConnection olconn = new OleDbConnection(connStr)) {
olconn.Open();
OleDbDataAdapter odp = new OleDbDataAdapter(commandText, olconn);
odp.Fill(dt);
}
}
catch (Exception ex)
{
}
}
return dt;
}
posted on 2011-12-16 17:42
sanmao 閱讀(767)
評論(0) 編輯 收藏