Warm tip: This article is reproduced from serverfault.com, please click

Cold Fusion 2018: how to save .xlsx file as .csv

发布于 2020-12-15 18:29:48

CF2018 on Win2019 Trying to save user-uploaded .xlsx file as .csv file to be processed into Oracle database. Running this code, which completes without errors, but the result is not legible:

<cfspreadsheet action = "read"
                format="csv"
                src="c:\bin\Nov_sales.xlsx"
                name="foo"
                > 

<cfspreadsheet action="write" 
                filename='c:\bin\Nov_sales.csv' 
                format="csv" 
                name="foo"  
                overwrite=true>

The result in .csv file looks like this:

504b 0304 1400 0808 0800 d260 8f51 0000
0000 0000 0000 0000 0000 0b00 0000 5f72
656c 732f 2e72 656c 73ad 92c1 4a03 3110
865f 25cc bd9b 6d05 1169 da8b 08bd 89d4
.....

What am I missing???

Questioner
epipko
Viewed
11
user12031119 2020-12-16 04:36:27

Presuming your excel file has a header record, what you should do is first read the excel file into a query object. From there, you can then export the query object into a csv by writing the file using a <cfoutput> loop.

<cfspreadsheet action="read" src="c:\bin\Nov_sales.xlsx" query="qryExcel">

<cfoutput query="qryExcel">
    <cfset line = "#col1#,#col2#,#col3#,#col4#,#col5#">
    <cffile action="append" file="c:\bin\Nov_sales.csv" output="#line#">
</cfoutput>