我有excel文件,可以在某些字段中包含逗号。如果我想使用cfspreadsheet
将文件转换为csv
,则可能会出现问题。我想知道replace
或convert
是否有通过\
的所有逗号的方式。在我替换所有逗号后,我将能够使用cfspreadsheet
创建csv
。这是我的代码如何读取我的文件:
<cfspreadsheet action = "read" format="csv" src="filePath\myFile.xlsx" name="csvvar">
如果有人可以帮忙解决这个问题,请告诉我。谢谢。
答案 0 :(得分:1)
从Excel转换为查询。然后,在每行的单元格数据中,替换&#34;,&#34; by&#34; \&#34;。像这样的东西
<cfspreadsheet
action = "read"
src="filePath\myFile.xlsx"
query="excelquery"
sheet="1">
<!--- Create CSV file in current directory--->
<cffile action="write" file="#expandpath('result.csv')#" output="">
<cfset columns = arraynew(1)>
<!--- Store the list of column names as an array --->
<cfset columns = listToArray(excelquery.ColumnList)>
<cfoutput query="excelquery">
<cfset rowList = "">
<cfloop from="1" to="#arraylen(columns)#" index="n">
<cfset colName = columns[n]>
<cfset cellData = evaluate("#colName#[currentrow]")>
<!--- Replace , by \ in each cell --->
<cfset cellData = replace(cellData, ",", "\", "all")>
<!--- Comma-separated row data --->
<cfset rowList = listAppend(rowList,cellData)>
</cfloop>
<!--- Place a carriage-return at the end of the row --->
<cfset rowList = rowList & '<br>'>
<!--- Append row to CSV file --->
<cffile action="append" file="#expandpath('result.csv')#" output="#rowList#" >
</cfoutput>