try
{
// strPath = ofd.FileName;
string strCon = "provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath+ "\\视觉班通信录.xlsx" + ";extended properties=excel 8.0";//关键是红色区域
OleDbConnection Con = new OleDbConnection(strCon);//建立连接
string strSql = "select * from [Sheet1$]";//表名的写法也应注意不同,对应的excel表为sheet1,在这里要在其后加美元符号$,并用中括号
OleDbCommand Cmd = new OleDbCommand(strSql, Con);//建立要执行的命令
OleDbDataAdapter da = new OleDbDataAdapter(Cmd);//建立数据适配器
DataSet ds = new DataSet();//新建数据集
da.Fill(ds, "shyman");//把数据适配器中的数据读到数据集中的一个表中(此处表名为shyman,可以任取表名)
//指定datagridview1的数据源为数据集ds的第一张表(也就是shyman表),也可以写ds.Table["shyman"]
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);//捕捉异常
}