TCL:表格(xls)中写入数据

时间:2023-03-08 17:32:29
 intToChar.tcl

 # input a number : 1 to 32 , you will get a char A to Z
#A-Z:1-32
proc intToChar {int} {
if {![string is integer $int]} {
return "Please input a number!"
}
if {![expr 0<$int&&32>$int]} {
return "Input a numer range 1 to 32!"
}
set listChars [list A B C D E F G H I J K L M N O P Q R S T U V W X Y Z]
set intTemp [expr $int-1]
return [lindex $listChars $intTemp]
} #test
#return [intToChar 1] intToChar.tcl package require tcom
source intToChar.tcl
proc xls_write {fileName date} {
# if {[file exists $fileName]} {
# return {File is existed ! Please delete or change the the of the file !}
# }
if {""==$date} {
return "The date is NULL or '' !"
}
# set fileName {D:/test.xlsx}
# 创建com实例,打开工作表
set application [::tcom::ref createobject "Excel.Application"]
$application Visible 1
set workbooks [$application Workbooks]
set workbook [$workbooks Add]
set worksheets [$workbook Worksheets]
#set worksheet [$worksheets Item "Sheet1"]
set worksheet [$worksheets Item [expr 1]]
set cells [$worksheet Cells] set exeFlag 1 if {1==$exeFlag} {
# set date {}
set dateRawLength [llength $date]
set rawIndex 0
while { $rawIndex < $dateRawLength } {
set dateColumnLength 0
set dateColumnLength [llength [lindex $date $rawIndex]]
set columnIndex 0
while { $columnIndex < $dateColumnLength } {
[$cells Item [expr $rawIndex+1] [intToChar [expr $columnIndex+1]]] Value2 [lindex [lindex $date $rawIndex] $columnIndex]
incr columnIndex
}
incr rawIndex
}
}
#code not work like wanting
# if {[regexp -all {\\|/} $fileName]} {
# cd [file dirname $fileName]
# }
#save without asking
$application DisplayAlerts [expr 0]
# $workbook SaveCopyAs {d://ts.xlsx}
$workbook SaveAs $fileName
$workbook Close
$application Quit
# return [file join [file dirname $fileName] [file tail $fileName]]
}
#含路径的文件保存时,有时会失败,原因不详(xls默认保存路径:菜单"文件"-->"选项"-->"保存",查看路径)
set fileName {d:\testaaa.xlsx}
set date [list [list 1 2 3 4 5 6 7 8 9 10] [list a b c d e f g] [list nihao hello 你好 howAreYou?]]
#test
return [xls_write $fileName $date]