通过ADO在Excel中添加行

问题描述 投票:0回答:1

我在使用ADO通过VBScript更新Excel工作表时遇到问题。

测试工作簿包含3个工作表:Sheet1,Sheet2和Sheet3。

我使用“INSERT”和“AddNew”方法来更新每个工作表。

如果标题行包含多个列,则标题行格式将保留在新行中。

如果标题行仅包含一列,则会跳过一行,但标题行格式不会保留在新行中。

这没有意义!任何援助将不胜感激。

附件是脚本和生成的工作表的图像。

    Option Explicit
'****
'*  Test updating (INSERT and ADDNEW) an MS-Excel worksheet using ADO.
'****
    Dim oADO
    Set oADO = CreateObject("ADODB.Connection")
        oADO.Mode = 3 '=adModeReadWrite
        oADO.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES;IMEX=1,ReadOnly=False';Data Source=excel.xls"
       '*
        oADO.Execute   "INSERT INTO [Sheet1$] (Comment) VALUES ('INSERT')"
       '*
        oADO.Execute   "INSERT INTO [Sheet2$] (Comment) VALUES ('INSERT')"
       '*
        oADO.Execute   "INSERT INTO [Sheet3$] (Comment) VALUES ('INSERT')"
    Dim oRST
    Set oRST = CreateObject("ADODB.Recordset")
        oRST.ActiveConnection = oADO
        oRST.CursorType = 2 '=adOpenDynamic
        oRST.LockType = 2 '= adLockPessimistic
       '*
        oRST.Open "SELECT * FROM [Sheet1$]"
        oRST.AddNew
        oRST.Fields("Comment").Value = "ADDNEW"
        oRST.Update
        oRST.Close
       '*
        oRST.Open "SELECT * FROM [Sheet2$]"
        oRST.AddNew
        oRST.Fields("Comment").Value = "ADDNEW"
        oRST.Update
        oRST.Close
       '*
        oRST.Open "SELECT * FROM [Sheet3$]"
        oRST.AddNew
        oRST.Fields("Comment").Value = "ADDNEW"
        oRST.Update
        oRST.Close
    Set oRST = Nothing
        oADO.Close
    Set oADO = Nothing

VBScript code and results in worksheets

excel vbscript ado
1个回答
0
投票

是因为文件已经存在并且那里已经存在一些空白数据了吗?我尝试了一个运行CREATE TABLE的脚本版本,在插入之前创建工作表,我没有看到空行。

Option Explicit

dim fso: set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists("excel.xls") then
    fso.DeleteFile "excel.xls"
end if


'****
'*  Test updating (INSERT and ADDNEW) an MS-Excel worksheet using ADO.
'****
Dim oADO
Set oADO = CreateObject("ADODB.Connection")
    oADO.Mode = 3 '=adModeReadWrite
    oADO.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES;IMEX=1,ReadOnly=False';Data Source=excel.xls"

oADO.Execute "CREATE TABLE Sheet1 (comment text)"
oADO.Execute "CREATE TABLE Sheet2 (comment text)"
oADO.Execute "CREATE TABLE Sheet3 (comment text)"


   '*
    oADO.Execute   "INSERT INTO [Sheet1$] (Comment) VALUES ('INSERT')"
   '*
    oADO.Execute   "INSERT INTO [Sheet2$] (Comment) VALUES ('INSERT')"
   '*
    oADO.Execute   "INSERT INTO [Sheet3$] (Comment) VALUES ('INSERT')"
Dim oRST
Set oRST = CreateObject("ADODB.Recordset")
    oRST.ActiveConnection = oADO
    oRST.CursorType = 2 '=adOpenDynamic
    oRST.LockType = 2 '= adLockPessimistic
   '*
    oRST.Open "SELECT * FROM [Sheet1$]"
    oRST.AddNew
    oRST.Fields("Comment").Value = "ADDNEW"
    oRST.Update
    oRST.Close
   '*
    oRST.Open "SELECT * FROM [Sheet2$]"
    oRST.AddNew
    oRST.Fields("Comment").Value = "ADDNEW"
    oRST.Update
    oRST.Close
   '*
    oRST.Open "SELECT * FROM [Sheet3$]"
    oRST.AddNew
    oRST.Fields("Comment").Value = "ADDNEW"
    oRST.Update
    oRST.Close
Set oRST = Nothing
    oADO.Close
Set oADO = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.