string excelFilePath = openFileDialog1.FileName;
//这里是弹出文件选取窗口为的是获取EXCEL文件的路径
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
OleDbConnection Oleconn = new OleDbConnection(strConn); string strExcel = "";
OleDbDataAdapter excelCommand = null;
DataSet excel_ds = new DataSet();
strExcel = "select * from [sheet1$]";
try { Oleconn.Open();
excelCommand = new OleDbDataAdapter(strExcel,Oleconn);
excelCommand.Fill(excel_ds,"exdtSource");//得到dataset }
catch (System.Exception ex)
{ MessageBox.Show("导入出错:" + ex, "错误信息"); }
finally { Oleconn.Close(); Oleconn.Dispose(); }