导入的表是 将excel导入到datagrid中 Dim strConn As String Dim strSQL As String Dim strDataFile As String Dim strTableName As String With cn .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " F:\test.xls& str1 & ";Extended Properties=Excel 8.0;Persist Security Info=False " .Open End With strSQL = "Select * from [Sheet1$]" adoRS.CursorLocation = adUseClient Set adoRS.ActiveConnection = cn adoRS.Open strSQL, cn, 2, 3 adoRS.MoveFirst Set DataGrid1.DataSource = adoRS.DataSource DataGrid1.Refresh 如何将datagrid不同的数据导入数据库中
按你的描述,这样试一下:dim sql as string dim rs_tb as adodb.recordsetwhile not rs_excel.eof sql="select [name] from tb where [name]='"& rs_excel!name &"'" set rs_tb =new adodb.recordset rs_tb.Open sql, cn, 2,3 if rs_tb.eof then sql="insert into tb([name],address,sex) " _ &"values('"& rs_excel!name &"','" & rs_excel!address &"','"& rs_excel!sex &"')" cn.execute sql end if set rs_tb=nothing rs_excel.movenext wend
Dim strConn As String
Dim strSQL As String
Dim strDataFile As String
Dim strTableName As String
With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " F:\test.xls& str1 & ";Extended Properties=Excel 8.0;Persist Security Info=False "
.Open
End With
strSQL = "Select * from [Sheet1$]"
adoRS.CursorLocation = adUseClient
Set adoRS.ActiveConnection = cn
adoRS.Open strSQL, cn, 2, 3
adoRS.MoveFirst
Set DataGrid1.DataSource = adoRS.DataSource
DataGrid1.Refresh
如何将datagrid不同的数据导入数据库中
楼主描述的有点乱吧两个表?数据库中的table和excel的sheet吗?说明白点儿这个应该算不上算法吧,很简单的
上面贴的代码是已经将excel打开了,并获得了recordset了,
我要解决的就是将excel的sheet导入到数据库中的table中excel中的数据比数据库表中的数据多,就是比较excel和数据库表之间的数据,如果excel中的某一行和数据库中的一样就跳过,如果不一样的话就 将excel中的数据导入到表中
就像这样
excel 的sheet1中 数据库中的表
姓名 地址 性别 姓名 地址 性别
李 北京 男 李 北京 男
张 北京 男 张 北京 男
赵 上海 女 吴 天津 女
导入后数据库中的表应该是
姓名 地址 性别
李 北京 男
张 北京 男
吴 天津 女
赵 上海 女
就是这样的问题
dim rs_tb as adodb.recordsetwhile not rs_excel.eof
sql="select [name] from tb where [name]='"& rs_excel!name &"'"
set rs_tb =new adodb.recordset
rs_tb.Open sql, cn, 2,3
if rs_tb.eof then
sql="insert into tb([name],address,sex) " _
&"values('"& rs_excel!name &"','" & rs_excel!address &"','"& rs_excel!sex &"')"
cn.execute sql
end if
set rs_tb=nothing
rs_excel.movenext
wend
太感谢你了vbman2003(家人)
也谢谢你上次的 指点datou985 (^\会急转弯儿的猪/^) 同时也多谢大家的关注 ,给分
vbman2003