excel - 为什么使用 AutoHotkey 附加到 xls 文件时会出现格式/扩展名错误?

标签 excel autohotkey

我附加到 .csv 文件时没有出现任何问题。

相反,我想附加到 .xlsx 文件,因为我希望能够制作一个漂亮的表格。

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

“Excel 无法打开文件“printlog.xls”,因为文件格式或文件扩展名无效。请验证文件是否已损坏且文件扩展名与文件格式匹配。”

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

AHK 网站上没有官方文档说明可以将 FileAppend 为 .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()

error image

如您所见,我收到的错误是非法字符。我不明白,因为变量的内容是字符串。这是否与变量本身的格式有关,或者是否与它附加到 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()

关于excel - 为什么使用 AutoHotkey 附加到 xls 文件时会出现格式/扩展名错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51862187/

相关文章:

excel - 具有 3 个条件的 IF 函数

excel - Excel错误1004 'No cells were found '

AutoHotkey:列出所有打开的窗口

automation - 在 AutoHotkey 中将剪贴板的内容作为击键发送

vba - 禁用图像 - Selenium VBA Chromedriver

excel - VBA,为什么粘贴我的数组只粘贴一个单元格

sql - 使用 Excel 编辑电子商务数据库是否现实?

powershell - 从 autohotkey 运行 powershell 脚本时执行策略错误

autohotkey - ControlClick 不起作用

audio - 自动热键 v2 : changing audio output (with NirCmd)