读取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
ApplicationClass ac = new ApplicationClass();
Workbook wk = ac.Workbooks.Add("D:\\1.xls");
Worksheet ws = (Worksheet)wk.ActiveSheet;
ac.Visible = true;
Range rg = ws.Cells;
然後就是把你感兴趣的数据弄到datagridview上了