通过ADO在Excel中添加行

时间:2018-08-28 19:42:37

标签: excel vbscript ado

我在通过VBScript使用ADO更新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

1 个答案:

答案 0 :(得分: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