我要做的是使用TCL脚本将文本添加到某个excel文件中。 对于使用excel我正在使用Tcom。
set excelFilePath "C:/TCL.Marko.xlsx"
proc start_tcom {} {
global excelFilePath excelApp workbook
set excelApp [::tcom::ref createobject Excel.Application]
set workbooks [$excelApp Workbooks]
set workbook [$workbooks Open [file nativename [file join [pwd] $excelFilePath] ] ]
set worksheets [$workbook Worksheets]
#
set excel_sheet Marko.xlsx
set worksheet [$worksheets Item [expr 1]]
set cells [$worksheet Cells]
return $cells
}
到目前为止一切正常。
509 % start_tcom
::tcom::handle0x0284F9D8
然后我正在跑步:
513 % foreach row {1 2 3} {
foreach column {A B C} {
$cells Item $row $column [incr i]
}
}
并收到此错误:
0x800a01a8 {Unknown error 0x800A01A8}
如果我在做的话
save_and_close_tcom
我可以看到文件Marko.xslx更新了正确的时间和日期,因此TCL看到了正确的文件,但仍然收到此错误。
答案 0 :(得分:0)
应删除“set excel_sheet Marko.xlsx”。 此命令意味着使用选项卡名称“Marko.xlsx”,但此选项卡不存在。这就是为什么错误来了。 这是正确的代码和解释:
proc start_tcom {} {
global excelFilePath excelApp workbook
set excelApp [::tcom::ref createobject Excel.Application]
# Connect to Excel COM object.
set workbooks [$excelApp Workbooks]
set workbook [$workbooks Open [file nativename [file join [pwd] $excelFilePath] ] ]
set worksheets [$workbook Worksheets]
# Create inital workbook.
set worksheet [$worksheets Item [expr 1]]
$worksheet Name "Booleans"
# Rename the first tab.
set cells [$worksheet Cells]
return $cells
#Making cells ready for input
}
设置fp [open“[pwd] /TEMP/WTX.txt”r] set file_data [read $ fp]
set Col A
set Row 1
set Row 1
while {[lindex $file_data $i] != ""} {
$cells Item $Row $Col $numeric_value_from_file
incr Row
}