日期:2014-05-19  浏览次数:20950 次

求Excel倒入sql server代码?
我现在有sql   server导出Excel的代码。看了还是不会写Excel倒入sql   server的。
希望谁有代码给我粘一下,让我学习一下   如果是sql   server导出Excel的那就算了   谢谢合作

谢谢

------解决方案--------------------
string mystring= "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = 'D:/ExportToExcel/excel/test.xls ';Extended Properties=Excel 8.0 ";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter( "select * from [Sheet1$] ",cnnxls);
DataSet myDs =new DataSet();
myDa.Fill(myDs);

if(myDs.Tables[0].Rows.Count > 0)
{
string strSql = " ";
string CnnString= "Provider=SQLOLEDB;database=testnews;server=(local);uid=sa;pwd= ";
OleDbConnection conn =new OleDbConnection(CnnString);
conn.Open ();
OleDbCommand myCmd =null;

for(int i=0; i <myDs.Tables[0].Rows.Count; i++)
{
strSql= "insert into news(title,body) values ( ' ";
strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + " ', ' ";
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + " ') ";

try
{
myCmd=new OleDbCommand(strSql,conn);
myCmd.ExecuteNonQuery();
Label8.Text = " <script language=javascript> alert( '数据导入成功. '); </script> ";
}
catch
{
Label8.Text = " <script language=javascript> alert( '数据导入失败. '); </script> ";
}
}
conn.Close();
}

}