我正在从我们的客户端接收Excel文件,我需要从它们创建一个CSV文件。我的问题是,Excel文件中的值不符合我们的标准,仅通过复制即可创建CSV文件。我需要在创建文件时以正确的顺序获取值。我阅读了CF的Livedocs,但找不到任何工作。
我一直在考虑创建一个结构并将数据拉出来,然后根据我的结构创建CSV文件,但我以前没有做过这样的事情,我不确定是否可能。我还考虑过使用ListGetat而不是结构,但仍未尝试过。
有人以前做过这样的事情吗?我正在尝试尽可能少的数字值,因为如果我们找到一个具有相同问题的客户,我会看到这将成为未来的问题。
更新(过去几天我更改了代码,所以这就是我目前拥有的)
<cfset DataDirectory = "E:testfoldertest.xlsx">
<cfspreadsheet action="read" src="#DataDirectory#" excludeHeaderRow="true" headerrow="1" query="queryData">
<cfquery name="contact" datasource="#ds#">
select ClientBrandID
from ClientBrands
where surveyReferralID IN
(select surveyReferralID from clientAdmin where username = '#res#')
</cfquery>
<cfscript>
dataFile = structNew();
StructInsert(datafile,"ClientBrandID",contact.ClientBrandID);
StructInsert(datafile,"surveyType", queryData.surveyType);
</cfscript>
<cfscript>
///We need an absolute path, so get the current directory path.
theFile= "E:testfolderNewTest.csv";
//Create a new Excel spreadsheet object and add the query data.
theSheet = SpreadsheetNew("PatientData");
SpreadsheetAddRow(theSheet, "ClientBrandID,SurveyType,Location,ClientContactID,FirstName,LastName,HomePhone,WorkPhone,CellPhone,Email"); <!---This is the header of the new CSV --->
SpreadsheetAddRows(theSheet, "datafile.clientbrandid, datafile.surveytype"); <!--- This is my latest failed attempt. Tried to pass in data from my struct, probably the quotes are the issue but haven't tried it without them --->
</cfscript>
<!--- Write the spreadsheet to a file, replacing any existing file. --->
<cfspreadsheet action="write" filename="#theFile#" format="csv" name="theSheet" overwrite=true >
所有这些操作都需要在CFLOOP中进行,才能浏览我的测试文件夹中的所有文件。现在,我正在硬编码一个文件,以便在做其他任何事情之前先解决问题。另外,我缺少在其中的另一个循环,需要浏览文件的所有值。它应该是 <cfloop query='queryData'>
之类的东西,这是我从cfspreadsheet中获取的查询。
您可以让它接受任何订单,但是您仍然需要将预期的列标题硬编码为数组或其他内容,并考虑它只是为了您的内部用途,我只会添加数据以其本应加入的顺序回到CSV。上次我从查询中做了一个电子表格,我这样做了。也许会有所帮助。
<cfscript>
sMySpreadSheet = spreadsheetNew();
column = 1;
<!--- header row --->
<!--- Remember what is expected from SpreadsheetSetCellValue --->
<!--- SpreadsheetSetCellValue(spreadsheetObj, value, row, column) --->
spreadsheetSetCellValue(sMySpreadSheet ,"First Name",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"Last Name",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"DOB",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"Phone Number",1,column); column++;
spreadsheetSetCellValue(sMySpreadSheet ,"Number of Kids",1,column); column++;
<!--- data rows --->
<!--- if you're not using a header row with real titles you can just use the
default col_x
example: spreadsheetSetCellValue(sMySpreadSheet , queryData.col_1[q], q+1, column); column++; --->
for(q=1; q LTE queryData.recordCount; q++){
column = 1;
spreadsheetSetCellValue(sMySpreadSheet , queryData.first[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.last[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.dob[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.phoneNumber[q], q+1, column); column++;
spreadsheetSetCellValue(sMySpreadSheet , queryData.kidCount[q], q+1, column);
}
<!--- make it purdy (optional) --->
spreadsheetFormatRow(queryData, {fgcolor="light_cornflower_blue"},1);
</cfscript>
如果您想简单地添加到CSV,则可以执行此类操作(将您的内容包裹在"如果您有资格):
<cfsetting enableCFoutputOnly = "Yes">
<cfsaveContent variable = "myCSV">
<cfset newline = #chr(13)#&#chr(10)#>
<cfoutput>First Name,Last Name,DOB,Phone Number,Number of Kids#newline#</cfoutput>
<cfoutput query="queryData">#queryData.first#,#queryData.last#,#queryData.dob#,#queryData.phoneNumber#,#kqueryData.idCount##newline#/cfoutput>
</cfsaveContent>
</cfsetting>
<cffile action = "append" file = "[your file]" output = "#myCSV#">
如果CFSaveContent会导致白色空间问题,并且CFSetting无济于事,这是另一种选择。
<cfset myCSV = "First Name,Last Name,DOB,Phone Number,Number of Kids#newline#">
<cfloop query="queryData">
<cfset myCSV &= "#queryData.first#,#queryData.last#,#queryData.dob#,#queryData.phoneNumber#,#queryData.kidCount##newline#">
</cfloop>
这是基于编辑的问题的一些建议。即使编辑的问题并不能清楚您要实现的目标。
关于"所有这些操作都需要在CFLOOP中进行,才能浏览我的测试文件夹中的所有文件",您可以在该文件夹上使用cfdirectory。它将返回一个可以通过循环的ColdFusion查询。
您的其余代码看起来比需要的要复杂。读取电子表格可以为您提供一个查询对象 - 到目前为止还不错。
您的数据库查询,名为Contact看起来很敏感。您指的是一个名为res的变量,但尚不清楚它来自何处。如果它来自电子表格,则值列表可能更合适。顺便说一句,使用查询参数。
似乎您希望您的CSV文件结合电子表格和数据库查询的数据。如果是这样,则查询可能有用。
如果是我,我会使用cffile action ="写"来创建我的CSV文件。如果其中任何一个包含逗号,我也会用双引号包围每个值。