一下代码不全,但是也差不多了,具体功能如题所示,如有更简洁方法,请麻烦留意一下.....
string connString;
connString = "server=.;uid=sa;pwd=;database=abc;";SqlConnection sConn = new SqlConnection(connString);
try
{
sConn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
int i;
string qy, xx, bj, xsxm, mobile, kfhm, jxtjrw, qjrh;
string file;
op.ShowDialog();
file = op.FileName;
op.Dispose();
if (file != "" && file.IndexOf("xls") != 0)
{
DataTable dt = new DataTable();
OleDbConnection MyConnection = new OleDbConnection("Provider =Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + file + " ;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'");
MyConnection.Open();
OleDbDataAdapter MyCommand = new OleDbDataAdapter("select iif(a is null,'',a) as a, iif(b is null,'',b) as b from [Sheet1$] ", MyConnection);
DataSet DS = new DataSet();
MyCommand.Fill(DS, "[Sheet1$]");
dt = DS.Tables["[Sheet1$]"];
MyConnection.Close();
MyConnection.Dispose();
for (i = 0; i < dt.Rows.Count; i++)
{
qy = Rs_dt(dt, i, "a");
xx = Rs_dt(dt, i, "b");
SqlCommand sCmd = new SqlCommand();
sCmd.CommandText = "insert into testv1(a,b) values('" + qy + "','" + xx + "')";
sCmd.Connection = sConn;
sCmd.CommandType = CommandType.Text;
sCmd.ExecuteNonQuery();
}
MessageBox.Show("导入数据成功");
}
}
catch (Exception ex)
{
MessageBox.Show("运行出错:" + ex.Message);
}
sConn.Close();