读取Excel首先你在引用中得引用Microsoft office Excel 20** 对应的dll文件,才能读取到Excel的数据
在类里面引用:System.Data.OleDb;
static public DataSet ExcelToDataSet(string filename)
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
ds = new DataSet();
myCommand.Fill(ds);
myConn.Close();
return ds;
}
这个方法返回一个数据读取的数据原给你的datagridview
温馨提示:答案为网友推荐,仅供参考