代码如下:
使用到的控件请自行添加!
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=tpc;server=(local);uid=sa;pwd=";
OleDbConnection conn =new OleDbConnection(CnnString);
conn.Open ();
OleDbCommand myCmd =null;
for(int i=1; i<myDs.Tables[0].Rows.Count; i++)
{
strSql="insert into tpch(FCust_No,FModel_No,FDc_No,FMeasure_Unit,FCurrency,FPrice,FInputDate,DelFlag) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[5].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[6].ToString() + "')";
try
{
myCmd=new OleDbCommand(strSql,conn);
myCmd.ExecuteNonQuery();
Label8.Text = "数据导入成功";
}
catch
{
Label8.Text = "数据导入失败";
}
}
conn.Close();
}