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]
xls_read.tcl
package require tcom
source intToChar.tcl
proc xls_read {fileName} {
if {![file exists $fileName]} {
return {File is not existed!}
}
# set fileName {D:/test.xlsx}
# 创建com实例,打开工作表
set application [::tcom::ref createobject "Excel.Application"]
$application Visible 0
set workbooks [$application Workbooks]
set workbook [$workbooks Open $fileName]
set worksheets [$workbook Worksheets]
#set worksheet [$worksheets Item "Sheet1"]
set worksheet [$worksheets Item [expr 1]]
set cells [$worksheet Cells]
set exeFlag 1
#version 1.1
if {1==$exeFlag} {
###result
#set result {}
set rowIndex 1
#used max rows count
set rowUsedCount [[[$worksheet UsedRange] Rows] Count]
puts "Max row is $rowUsedCount!"
#used max columns count
set columnsUsedCount [[[$worksheet UsedRange] Columns] Count]
puts "Max Column is $columnsUsedCount!"
while { $rowIndex <= $rowUsedCount } {
set rowResult {}
set columnIndex 1
while { $columnIndex <= $columnsUsedCount } {
set value [[$cells Item $rowIndex [intToChar $columnIndex]] Value]
# puts $rowNumber+$columnNumber
#show every value
# puts $columnValue
lappend rowResult $value
#next column
incr columnIndex
}
puts $rowResult
###result
#lappend result $rawResult
#read next raw:init orgin vlaue
#next raw
incr rowIndex
}
}
#version 1.0
if {-1==$exeFlag} {
###result
#set result {}
set rawEnd 0
set columnEnd 0
set rowNumber 1
set columnNumber 1
while { $rawEnd == 0 } {
set rawResult {}
# set columnNumber 1
while { $columnEnd == 0 } {
set columnValue [[$cells Item $rowNumber [intToChar $columnNumber]] Value]
# puts $rowNumber+$columnNumber
#check column isEnd
if { $columnValue == ""} {
set columnEnd 1
continue
}
#show every value
# puts $columnValue
lappend rawResult $columnValue
#next column
incr columnNumber
}
puts $rawResult
###result
#lappend result $rawResult
#read next raw:init orgin vlaue
set columnEnd 0
set columnNumber 1
#next raw
incr rowNumber
#check row isEnd
if {[[$cells Item $rowNumber A] Value] == ""} {
set rawEnd 1
continue
}
}
}
$workbook Close
$application Quit
###result
#return $result
}
set filename {D:/test.xlsx}
#test
return [xls_read $filename]