把Excel转换成DataTable

public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)



{     



string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" +"Extended Properties=Excel 5.0;";     



string strExcel = string.Format("select * from [{0}$]", strSheetName);  



    DataSet ds = new DataSet();     



using (OleDbConnection conn = new OleDbConnection(strConn))   



  {         



conn.Open(); 



        OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);     



    adapter.Fill(ds, strSheetName);   



      conn.Close();     



}     



return ds.Tables[strSheetName];}

 

你可能感兴趣的:(Datatable)