用C#如何读取EXCEL表格中的数据,再导入我数据库中,
1. 建立连接
//注意,"Extended Properties"是必需的
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Book1.xls") + ";Extended Properties=\"Excel 8.0;\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
2. 执行操作
//注意,列名是Excel中的第一行数据
string strCommand = "update [Sheet1$] set col1='aa22' where col1='a2'";
OleDbCommand cmd = new OleDbCommand(strCommand, conn);
cmd.ExecuteNonQuery();
3. 读取数据
OleDbDataAdapter da = new .OleDbDataAdapter("select * from [Sheet1$]", conn);
DataSet ds = new DataSet();
da.Fill(ds);
4.存入数据库
后来才发现这个速度很慢!