Excel users: here's a proc to convert XLS files to CSV format (correction) 
Author Message
 Excel users: here's a proc to convert XLS files to CSV format (correction)

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

Quote:
}

# }}}

Enjoy.
Dobeedo

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



Sat, 11 May 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Excel users: here's a proc to convert XLS files to CSV format

2. Excel users: here's a proc to decode CSV data lines

3. Excel xls file format

4. Fortran/C reading Excel (.xls) files

5. Read Excel (xls) files

6. Read .xls (Excel) files

7. convert txt excel file without open the excel application

8. reading Excel .csv files

9. saving a excel sheet as a csv file

10. dictionary files in csv format

11. Text File To Windows CSV format

12. Converting a CSV file

 

 
Powered by phpBB® Forum Software