public static DataSet ImportExcel() { DataSet ds = new DataSet(); string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\link.xlsx;Extended Properties=Excel 12.0"; OleDbConnection Conn = new OleDbConnection(strConn); Conn.Open(); OleDbDataAdapter data = new OleDbDataAdapter("select * from [Sheet1$]", strConn); data.Fill(ds); return ds; }
针对是2007版的EXCEL
如果写相对路径的话:
DataSet ImportExcel() { string n = Server.MapPath("link.xlsx"); DataSet ds = new DataSet(); string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+n+";Extended Properties=Excel 12.0"; OleDbConnection Conn =new OleDbConnection(strConn); Conn.Open(); OleDbDataAdapter data = new OleDbDataAdapter("select * from [Sheet1$]", strConn); data.Fill(ds); return ds; }
上面是针对web,如果是控制台应用程序的话,它本身没有 Server.MapPath:
public static DataSet ImportExcel() { string url ="\\link.xlsx"; DataSet ds = new DataSet(); string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+url+";Extended Properties=Excel 12.0"; OleDbConnection Conn = new OleDbConnection(strConn); Conn.Open(); OleDbDataAdapter data = new OleDbDataAdapter("select * from [Sheet1$]", strConn); data.Fill(ds); return ds; }