用ADO.NET从Excel文件中读取数据
1.用openrowset,与connection无关
2.用OleDb
倾向与第一种
SqlDataAdapter command = new SqlDataAdapter(
"select * from openrowset('Microsoft.Jet.OLEDB.4.0','EXCEL 8.0;HDR=YES;User id=admin;Password=;IMEX=1;DATABASE=" +
XlsPath + "', [" + SheetName + "$])", connection);
command.Fill(ds, "ds");
"select * from openrowset('Microsoft.Jet.OLEDB.4.0','EXCEL 8.0;HDR=YES;User id=admin;Password=;IMEX=1;DATABASE=" +
XlsPath + "', [" + SheetName + "$])", connection);
command.Fill(ds, "ds");
2.用OleDb
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + XlsPath + ";Extended Properties=Excel 8.0;IMEX=1;";
string strExcelSql = "";
DataSet ds = null;
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = null;
conn.Open();
strExcelSql = "select * from [" + SheetName + "$]";
myCommand = new OleDbDataAdapter(strExcelSql, strConn);
ds = new DataSet();
myCommand.Fill(ds, strSheetName);
conn.Close();
string strExcelSql = "";
DataSet ds = null;
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = null;
conn.Open();
strExcelSql = "select * from [" + SheetName + "$]";
myCommand = new OleDbDataAdapter(strExcelSql, strConn);
ds = new DataSet();
myCommand.Fill(ds, strSheetName);
conn.Close();
倾向与第一种