Karan Nayyar
Karan Nayyar

Reputation: 760

How to read a xlsx worksheet file in tcl?

Currently, I am reading a file by converting it to CSV format. Is there any way we can read an excel worksheet by tcl?

Upvotes: 1

Views: 3662

Answers (1)

Mkn
Mkn

Reputation: 648

Below example with CAWT package to open a csv file and get its values:

package require cawt

# Open a new instance
set appId [Excel::OpenNew false] ; # set true to open an visible Excel
set workbookId [Excel::AddWorkbook $appId]

set csvfile {c:\temp\test.csv}
# 1;2;3
# 4;5;6

try {

    set worksheetId [Excel::GetWorksheetIdByIndex $workbookId 1] ; # open first sheet
    set rangeId [Excel::SelectRangeByString $worksheetId "A1"]   ; # select range 'A1'
    
    # Import csv file to range A1
    Excel::Import $rangeId $csvfile \
           -consecutive false \
           -delimiter ";" \
           -decimalseparator "."
    
    # Get last column & row used...
    set numCols [Excel::GetNumUsedColumns $worksheetId]
    set numRows [Excel::GetNumUsedRows $worksheetId]
    
    # Get values 
    puts [Excel::GetMatrixValues $worksheetId 1 1 $numRows $numCols]
    
} finally {
    
    # no need to close Excel if visible...
    if {![Excel::Visible $appId]} {
        Excel::Close $workbookId
        Excel::Quit $appId false
    } else {
        Cawt::Destroy ; # delete com object
    }
}

Result :

{1.0 2.0 3.0} {4.0 5.0 6.0}

Upvotes: 1

Related Questions