Subject: Excel users: here's a proc to convert XLS files to CSV format (correction) - DN [1]


Dobeedo <dobeedo@my-deja.com> - 23 Nov 1999 - comp.lang.tcl

 Dear all,
 I wrote a little proc that uses the tcom extension to convert XLS files
 to the CSV format (Comma Separated Format) by calling Excel but without
 the need for the user to interact with Excel. Thought other people
 would have the same need. So here's the proc:

 # location of MS Office .OLB files
 set msofficepath {/Microsoft Office 97/office}

 # {{{ xls_to_csv

 # Convert file from XLS to CSV formats
 # RETURNS name of CSV-formatted output file

 proc xls_to_csv {infile} {
     global msofficepath

     # work with temporary files (Excel may expects backslashes)
     set xl_infile "\\temp\\tcom_[pid].xls"
     set xl_outfile "\\temp\\tcom_[pid].csv"

     if ![file exists $infile] {
     error "xls_to_csv: infile \"$infile\" does not exists"
     }

     file copy $infile $xl_infile

     # compute outfile name
     set outfile "[file rootname $infile].csv"

     package require tcom

     ::tcom::import "$msofficepath/excel8.olb"
     set application [::tcom::bind "Excel.Application"]
     $application Visible 1
     set workbooks [$application Workbooks]
     set workbook [$workbooks Open $xl_infile]
     # save file in CSV format
     file delete $xl_outfile
     $workbook SaveAs $xl_outfile $::Excel::XlFileFormat(xlCSV)
     # Avoid warning dialog.
     $workbook Saved 1
     # close workbook without saving changes (avoid warning message)
     $workbook Close "0"
     # quit Excel
     $application Quit
     ::tcom::release $workbook $workbooks $application

     file copy -force $xl_outfile $outfile
     file delete -force $xl_outfile $xl_infile

     return $outfile
 }

 # }}}

 Enjoy.
 Dobeedo

 Sent via Deja.com http://www.deja.com/
 Before you buy.

Last modified
1999-12-10

(195.108.246.50)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated