1 //excel模板的路径 2 string strSource = Application.StartupPath + "\" + "ExcelTemplet" + ".xls"; 3 4 DataTable dtGetExcel = new DataTable(); 5 6 dtGetXls = XlsToDataTable(strSource); 7 8 /// <summary> 9 /// 将Excel中的数据导入到DataTable中,获取EXCEL中列的顺序 10 /// </summary> 11 /// <param name="strpath">excel模板的路径</param> 12 /// <returns></returns> 13 private DataTable XlsToDataTable(String strpath) 14 { 15 string strConn; 16 strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'",strpath); 17 18 OleDbConnection conn = new OleDbConnection(strConn); 19 conn.Open(); 20 OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn); 21 DataTable dt = new DataTable(); 22 myCommand.Fill(dt); 23 conn.Close(); 24 25 return dt; 26 }