Geo*_*Geo 3 coldfusion coldfusion-9
我从我们的客户端收到excel文件,我需要从他们创建一个csv文件.我的问题是excel文件中的值与我们的标准不匹配,只是通过复制它来创建csv文件.在创建文件时,我需要获取值并将它们按正确顺序放置.我通读了liveocs的CF,但是找不到任何有效的东西.
我正在考虑创建一个结构并将数据拉出来,然后根据我的结构创建csv文件,但我之前没有做过这样的事情,我不确定它是否可能.我还想过使用listGetAt而不是结构但仍然没有尝试过.
以前有人这样做过吗?我正在努力将硬编码作为尽可能少的值,因为如果我们得到第二个具有相同问题的客户端,我认为这将成为未来的问题.
更新(过去几天我改变了我的代码,所以这就是我现在所拥有的)
<cfset DataDirectory = "E:\testfolder\test.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:\testfolder\NewTest.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 >
Run Code Online (Sandbox Code Playgroud)
所有这些操作都需要在cfloop中才能遍历我的test文件夹中的所有文件.现在,在我做其他事情之前,我正在硬编码单个文件以解决手头的问题.此外,我错过了另一个需要遍历文件的所有值的循环.这应该是<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>
Run Code Online (Sandbox Code Playgroud)
如果你想简单地添加到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#">
Run Code Online (Sandbox Code Playgroud)
如果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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2741 次 |
最近记录: |