为什么在使用 AutoHotkey 附加到 xls 文件时出现 format/extension 错误?

Why am I getting a format/extension error when appending to an xls file with AutoHotkey?

我附加到 .csv 文件没有问题。

相反,我想附加到 .xlsx 文件,因为我希望能够制作出漂亮的 table.

不幸的是,在打开 xlsx 文件时,我收到此消息:

"Excel cannot open the file 'printlog.xls' because the file format or the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

它不会对 csv 文件执行此操作。

AHK网站上没有官方文档说可以FileAppend to .xlsx格式,但是有很多人们这样做的在线示例数量。

这是我的代码:

SetWorkingDir %A_ScriptDir%

ControlGetText, test, Edit10, TeamViewer

filename = %A_ScriptDir%\printlog.xlsx

FileAppend,
(
%test%,%test%,%test%,%test%
%test%,%test%,%test%,%test%
), %filename%

更新:由于@user3419297 的解决方案,第一部分已经解决。我有下面列出的新问题。

FilePath := A_ScriptDir "\printlog.xlsx"   ; get file path
SetWorkingDir %A_ScriptDir%                ; set working directory

ControlGetText, itemcode, Edit1, Print -   ; get item code from print menu
ControlGetText, quantity, Edit2, Print -   ; get quantity from print menu
ControlGetText, amount, Edit3, Print -     ; get amount from print menu
ControlGetText, initials, Edit4, Print -   ; get quantity from print menu
ControlGetText, info, Edit5, Print -       ; get quantity from print menu
ControlGetText, batch, Edit6, Print -      ; get quantity from print menu

Formattime,ts,,Shortdate                   ; generate short date variable

oExcel := ComObjCreate("Excel.Application")  ; create Excel COM object
oWorkbook := oExcel.Workbooks.Open(FilePath) ; open workbook in Excel object
oExcel.Visible := false                      ; work in background

                                               ; check for blank rows and append values
For Row in oExcel.Range["A1"]                  ; starting with the first row
{
        If (Row.Value = "")                        ; if first row is blank
        {
                oExcel.Range("A1").Value := %itemcode% ; set value of cell A-G
                oExcel.Range("B1").Value := %quantity%
                oExcel.Range("C1").Value := %amount%
                oExcel.Range("D1").Value := %initials%
                oExcel.Range("E1").Value := %info%
                oExcel.Range("F1").Value := %batch%
                oExcel.Range("G1").Value := %ts%
        }
        else ; else find the next blank row and set values of A-G
        For eachRow in oExcel.Range["A1:A" oExcel.Columns("A").Find[""].Row]
        {
        If (eachRow.Value = "")
            {
                        oExcel.Range("A" . A_Index).Value := %itemcode%
                        oExcel.Range("B" . A_Index).Value := %quantity%
                        oExcel.Range("C" . A_Index).Value := %amount%
                        oExcel.Range("D" . A_Index).Value := %initials%
                        oExcel.Range("E" . A_Index).Value := %info%
                        oExcel.Range("F" . A_Index).Value := %batch%
                        oExcel.Range("G" . A_Index).Value := %ts%
                        break
            }
        }
}
oWorkbook.Save() ; save workbook and close excel
oExcel.Quit()

如您所见,我收到的错误是非法字符。我不明白,因为变量的内容是字符串。这与变量本身的格式有关,还是它被附加到 excel 文件的格式?

要将文本附加到 .xlsx 文件,您必须使用 COM:

test := "my text"

FilePath := A_ScriptDir "\printlog.xlsx"
oExcel := ComObjCreate("Excel.Application")
oWorkbook := oExcel.Workbooks.Open(FilePath)
oExcel.Visible := false
oExcel.Range("A1").Value := test
oWorkbook.Save()
oExcel.Quit()

另一个例子:

将复制文本的前 3 个单词附加到它们各自的列 a、b、c 中的下一个空行:

FilePath := A_ScriptDir "\printlog.xlsx"

Array:=[]
Array := StrSplit(clipboard, " ")

oExcel := ComObjCreate("Excel.Application")
oWorkbook := oExcel.Workbooks.Open(FilePath)
oExcel.Visible := false
For Row in oExcel.Range["A1"]
{
    If (Row.Value = "")
     {
        oExcel.Range("A1").Value := Array[1]
        oExcel.Range("B1").Value := Array[2]
        oExcel.Range("C1").Value := Array[3]
    }
    else
    For eachRow in oExcel.Range["A1:A" oExcel.Columns("A").Find[""].Row]
    {
     If (eachRow.Value = "")
       {
            oExcel.Range("A" . A_Index).Value := Array[1]
            oExcel.Range("B" . A_Index).Value := Array[2]
            oExcel.Range("C" . A_Index).Value := Array[3]
                break
        }
    }
}
oWorkbook.Save()
oExcel.Quit()

关于你的第二个截图问题很简单,因为我做了很多次...

这个:

oExcel.Range("A1").Value := %itemcode%

意思是:取名字为变量itemcode内容的变量的值("this. is. a. test."我猜?)。

它不适用于你的情况。 示例:

itemcode := "A B"
msgbox %itemcode% ; display will be : A B

所以这个 :

Test := %itemcode%

等同于:

Test = %A B%

未编译。 :-) 所以你需要删除 % % 除非你有动态变量(很少有这种情况)所以你将它们的名字存储在另一个变量中(varception?)。

oExcel.Range("A1").Value := itemcode