我有一个查询,我正在循环并附加到数组。循环完成后,我使用带有制表符分隔符的ArrayToList()
。问题是每行之间我有一个额外的空列。我的猜测是该命令将最后一个逗号转换为制表符分隔符。我仍然找不到防止这种情况的方法。这是我的代码:
<cfspreadsheet action="read" src="#urlPath#\Roster.xlsx" query="excelData" headerrow="1">
<cfset metadata = getMetaData(excelData)>
<cfset colList = "">
<cfloop index="col" array="#metadata#">
<cfset colList = listAppend(colList, col.name)>
</cfloop>
<cfset buffer=ArrayNew(1)>
<cfset crlf=chr(13)&chr(10)/>
<cfoutput query="excelData">
<cfloop index="i" list="#colList#">
<cfset ArrayAppend(buffer,excelData[i][currentRow])>
</cfloop>
//prevents blank row to be created on the end
<cfif excelData.currentrow eq excelData.recordcount>
<cfset ArrayAppend(buffer,"")>
<cfelse>
<cfset ArrayAppend(buffer,crlf)>
</cfif>
</cfoutput>
//setting tab delimiter
<cfset test = arrayToList(buffer,chr(9))/>
<cfscript>
FileWrite("#urlPath#\Test_Text.txt", "#test#");
</cfscript>
以下是运行此代码后的输出:
以下是我需要输出的内容:
我选择ArrayAppend()
,因为它比ListAppend()
快得多,如果有人可以帮助解决这个问题请告诉我。谢谢。
如果我做缓冲区数组的cfdump,这就是输出:
array
1 LAST_NAME
2 FIRST_NAME
3 BIRTH_DATE
4
5 Milos
6 TestOne
7 11/16/97
8
看起来每行之间是空行第4行和第8行。这就是我修复日期格式问题的方法:
<cfoutput query="excelData">
<cfset thisRow = []>
<cfset count = 1>
<cfloop index="i" list="#colList#">
<cfif count EQ 3 AND excelData[i][currentRow] NEQ "BIRTH_DATE">
<cfset ArrayAppend(thisRow,dateFormat(excelData[i][currentRow],"mm/dd/yyyy"))>
<cfelse>
<cfset ArrayAppend(thisRow,excelData[i][currentRow])>
</cfif>
<cfset count++>
</cfloop>
<cfset ArrayAppend(buffer,arrayToList(thisRow, chr(9)))>
</cfoutput>
答案 0 :(得分:4)
例如,您的数组是
['Doe', 'John', '1/1/1900',crlf,'Doe','Jane','1/1/1900']
如果在每个值之间放置一个标签,则crlf
之前和之后都会有一个标签。
我建议为工作表中的每一行创建一个字符串以插入缓冲区,然后创建一个以crlf
作为分隔符的arrayToList,同时不再需要if语句。
<cfspreadsheet action="read" src="#urlPath#\Roster.xlsx" query="excelData" headerrow="1">
<cfset metadata = getMetaData(excelData)>
<cfset colList = "">
<cfloop index="col" array="#metadata#">
<cfset colList = listAppend(colList, col.name)>
</cfloop>
<cfset buffer=ArrayNew(1)>
<cfset crlf=chr(13)&chr(10)/>
<cfoutput query="excelData">
<cfset thisRow = []>
<cfloop index="i" list="#colList#">
<cfset ArrayAppend(thisRow,excelData[i][currentRow])>
</cfloop>
<cfset ArrayAppend(buffer, arrayToList(thisRow,chr(9)))>
</cfoutput>
// joining to list on crlf
<cfset test = arrayToList(buffer, crlf)/>
<cfscript>
FileWrite("#urlPath#\Test_Text.txt", "#test#");
</cfscript>
只是因为:(未经测试)
<cfspreadsheet action="read" src="#urlPath#\Roster.xlsx" query="excelData" headerrow="1">
<cfscript>
metadata = getMetaData(excelData);
buffer = [];
crlf = chr(13)&chr(10);
for (data in excelData) {
thisRow = ArrayMap(metadata, function (col,i) {
return data[col];
});
ArrayAppend(buffer, arrayToList(thisRow,chr(9)));
}
test = arrayToList(buffer, crlf);
FileWrite("#urlPath#\Test_Text.txt", "#test#");
</cfscript>