日期:2014-05-17 浏览次数:20410 次
DataTable dt = new Helps().GetExcelData(path);
public System.Data.DataTable GetExcelData(string strPath)
{
System.Data.DataTable dt = new System.Data.DataTable();
try
{
DataSet ds = new DataSet();
//string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;";
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter myCommand = null;
string strExcel = "select * from [Sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(dt);
conn.Dispose();
}
catch (Exception ex)
{
ex.ToString();
dt = null;
}
return dt;
}