VB中使用adodb更新excel中内容遇到的有关问题
VB中使用adodb更新excel中内容遇到的问题
Dim excelConn1 As New ADODB.Connection
excelConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Mode=ReadWrite;Data Source=" & excelFile & ";Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1;"""
excelConn.Open
Dim excelRec As ADODB.Recordset
excelRec.Open "select * from [01公共设施$]", excelConn, 1, 3
excelRec(21) = “new” 这里出错提示"不能更新。数据库或对象为只读。"
excelRec.Update
应该是哪里是只读的,但是我找不到在哪儿改,看了个帖子在连接字中加"ReadOnly=False;" 我加了就报错
------解决方案--------------------
和你分享一下源码,有需要订做软件的话叫我
Dim excelConn1 As New ADODB.Connection
excelConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Mode=ReadWrite;Data Source=" & excelFile & ";Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1;"""
excelConn.Open
Dim excelRec As ADODB.Recordset
excelRec.Open "select * from [01公共设施$]", excelConn, 1, 3
excelRec(21) = “new” 这里出错提示"不能更新。数据库或对象为只读。"
excelRec.Update
应该是哪里是只读的,但是我找不到在哪儿改,看了个帖子在连接字中加"ReadOnly=False;" 我加了就报错
------解决方案--------------------
和你分享一下源码,有需要订做软件的话叫我
- VB code
Private Sub Form_Load() Dim RS As ADODB.Recordset Set RS = GetExcelRs(App.Path & "\book1.xls") MsgBox RS.RecordCount MsgBox RS(0) RS(0) = Timer RS.Update RS.Close End Sub '┏〓〓〓〓〓〓〓〓〓 GetExcelRs,start 〓〓〓〓〓〓〓〓〓┓ '[简介]: 'VB读取EXCEL工作薄某个表中数据 Function GetExcelRs(ByVal sFile As String, Optional ExcelSheetName As String = "sheet1", Optional ErrInfo As String) As ADODB.Recordset '[mycode_id:2025],edittime:2011-9-7 下午 02:15:41 On Error GoTo Err Dim RS As ADODB.Recordset Set RS = New ADODB.Recordset Dim ConnStr As String ConnStr = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & sFile & ";ReadOnly=False" RS.Open "SELECT * FROM [" & ExcelSheetName & "$]", ConnStr, 1, 3 Set GetExcelRs = RS Set RS = Nothing Exit Function Err: ErrInfo = Err.Description MsgBox ErrInfo End Function '┗〓〓〓〓〓〓〓〓〓 GetExcelRs,end 〓〓〓〓〓〓〓〓〓┛
------解决方案--------------------
IMEX=1
改为
IMEX=2就可以了
IMEX=0:写
IMEX=1:读
IMEX=2:读写