问题描述
运行出错如上图,代码如下:DimfilepathAsStringDimfilenameAsStringOpenFileDialog1.ShowDialog()filepath=OpenFileDialog1.InitialDirectoryfilename=OpenFileDialog1.FileNameTextBox2.Text=filenameIfMsgBox("你真的要导入EXCEL文件中的数据吗?",vbYesNo)<>vbYesThenExitSubDimcnAsNewADODB.ConnectionDimrsAsNewADODB.RecordsetDimstrAsStringDimcn1AsNewADODB.ConnectionDimcmdAsNewADODB.CommandDimrs1AsNewADODB.Recordset'cn.ConnectionString="driver={microsoftexceldriver(*.xls)};dbq="&App.Path&"department.xls"'此为原来的同一目录的语句,不删留作参考cn.ConnectionString="driver=microsoftexceldriver(*.xlsx,*.xls);dbq="&filepath&filename'cn.ConnectionString="driver=microsoftexceldriver(*.xls);dbq=01diyuan.xls"'cn1.ConnectionString="HDR=Yes;ExtendedProperties=Excel12.0;Provider=Microsoft.Ace.OLEDB.12.0;DataSource=01duyuan.xls"'cn1.ConnectionString="select*fromOpenDataSource('Microsoft.Jet.OLEDB.4.0;DataSource=01diyuan.xls;ExtendedProperties=Excel8.0"cn.Open()rs.Open("select*from[Sheet1$]",cn,1,1)cn1.ConnectionString="Provider=sqloledb;DataSource=master;InitialCatalog=NewStu;UserId=sa;Password=qiyungang1230;"cn1.Open()rs1.CursorLocation=ADODB.CursorLocationEnum.adUseClientrs1.Open("select*fromNewStu",cn1,1,2)'cmd.CommandText="deletefromysfpdepartment"'清空原数据表'cn1.Execute(cmd.CommandText)Forj=1Tors.RecordCountIfrs.EOF=TrueThenExitSubElsers1.AddNew()Fori=0Tors.Fields.Count-1rs1.Fields(i).Value=rs.Fields(i).ValueNextirs1.Update()rs.MoveNext()EndIfNextjcn.Close()cn1.Close()MsgBox("导入数据成功!",,"提示窗口")EndSub
解决方案
解决方案二:
是否Excel版本的引用的组件问题供参考!
解决方案三:
我现在在学VB,觉得很难啊,怎样才能快速学会VB呢?
解决方案四:
1.首先确定office版本Ifis07or03=TrueThen'07strConn=("Provider=Microsoft.Ace.OleDb.12.0;"&"datasource=")+path_file&";ExtendedProperties='Excel12.0;HDR=NO;IMEX=1'"ElsestrConn="Provider=Microsoft.Jet.OLEDB.4.0;DataSource='"&path_file&"';ExtendedProperties=Excel8.0;"EndIf2.查看office组件是否完整3.如果上面两个都没问题建议检查代码
解决方案五:
还可以这样导入啊,学习了我都是把表格内容转为数组,然后写进去
解决方案六:
试试1.rs.Open("select*from[“&Sheet1&”]",cn,1,1)2.rs1.Open("select*fromNewStu",cn1,1,2)重新确认一下