cfspreadsheet 另存为 .csv,Excel 表示 "The file format and extension of FILE.csv don't match."
cfspreadsheet save as .csv, Excel says "The file format and extension of FILE.csv don't match."
我创建了一个冷聚变页面,用于将 MYSQL 中的客户列表输出到 CSV 文件中,以便轻松上传到 SalesForce.com
我可以生成包含所有正确信息的文件。但是,当我尝试使用 excel 打开它时,出现错误:
"The file format and extension of 'SalesForceDailyLeads-20160613125138.csv' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?" 我可以打开它(excel for MAC),但在我看来,CFSpreadsheet 没有创建合法的 .csv 文件,而是创建了一个 xlsx。
<cfset FileCSV = "SalesForceDailyLeads-#dateformat(getBatch.BATCH,"yyyymmdd")##timeformat(getBatch.BATCH,"HHmmss")#.csv" >
<cfset filename = "/SF/#fileCSV#">
<cfset s = spreadsheetNew() >
<cfset spreadsheetAddRow(s, "FIRST, LAST, MIDDLE, STREET, CITY, ZIP, STATE")>
<cfinclude template="SFgetList.cfm">
<cfset spreadsheetAddRows(s, getList)>
<cfspreadsheet
action="write"
overwrite = "true"
format ="csv"
name ="s"
filename ="#filename#"
>
如果我创建一个 XLS 文件,我不会像使用 CSV 文件那样遇到问题。这是代码、CFSpreadsheet 或 excel(对于 mac)的问题吗?我可以修复它吗?
使用 cffile 而不是 cfspreadsheet 创建文件。每 the documentation:
The cfspreadsheet tag writes only XLS[X] format files. To write a CSV
file, put your data in a CSV formatted string variable and use the
cffile tag to write the variable contents in a file.
谢谢!当然,在我发布这篇文章之后,我立即找到了文档,发现我使用不正确,所以我改用了 CFFILE。我使用了我发现的 script/function 来做到这一点。然而,将查询转换为 CSV 需要一些工作——幸运的是,其他人已经做到了。如果有人想看到它:
我在这里得到了 querytoCSV 脚本:
https://gist.github.com/CreativeNotice/2775372
<cfscript>
/**
* queryToCsv
* Allows us to pass in a query object and returns that data as a CSV.
* This is a refactor of Ben Nadel's method, http://www.bennadel.com/blog/1239-Updated-Converting-A-ColdFusion-Query-To-CSV-Using-QueryToCSV-.htm
* @param {Query} q {required} The cf query object to convert. E.g. pass in: qry.execute().getResult();
* @param {Boolean} hr {required} True if we should include a header row in our CSV, defaults to TRUE
* @param {String} d {required} Delimiter to use in CSV, defaults to a comma (,)
* @return {String} CSV content
*/
public string function queryToCsv(required query q, required boolean hr = true, required string d = ","){
var colNames = listToArray( lCase(arguments.q.columnlist) );
var newLine = (chr(13) & chr(10));
var buffer = CreateObject('java','java.lang.StringBuffer').Init();
// Check if we should include a header row
if(arguments.hr){
// append our header row
buffer.append(
ArrayToList(colNames,arguments.d) & newLine
);
}
// Loop over query and build csv rows
for(var i=1; i <= arguments.q.recordcount; i=i+1){
// this individual row
var thisRow = [];
// loop over column list
for(var j=1; j <= arrayLen(colNames); j=j+1){
// create our row
thisRow[j] = replace( replace( arguments.q[colNames[j]][i],',','','all'),'""','""""','all' );
}
// Append new row to csv output
buffer.append(
JavaCast( 'string', ( ArrayToList( thisRow, arguments.d ) & iif(i < arguments.q.recordcount, "newLine","") ) )
);
}
return buffer.toString();
};
</cfscript>
<cfinclude template="getDups.cfm">
<cfinclude template="SFgetList.cfm">
<cfset FileCSV = "SalesForceDailyLeads-#dateformat(getBatch.BATCH,"yyyymmdd")##timeformat(getBatch.BATCH,"HHmmss")#.CSV" >
<cfset filename = "/mnt/nas-share/data/feed/SF/#fileCSV#">
<cfset qc = #queryToCsv(getList, false, ",")# >
<cfoutput>#qc#</cfoutput>
<cfset heads= "FIRST, LAST, MIDDLE, STREET, CITY, ZIP, STATE">
>
<cffile
action = "write"
file = #filename#
output = #heads#
addNewLine = "yes"
fixnewline = "no">
<cffile
action = "append"
file = #filename#
output = #qc#
addNewLine = "yes"
fixnewline = "no">
我创建了一个冷聚变页面,用于将 MYSQL 中的客户列表输出到 CSV 文件中,以便轻松上传到 SalesForce.com
我可以生成包含所有正确信息的文件。但是,当我尝试使用 excel 打开它时,出现错误: "The file format and extension of 'SalesForceDailyLeads-20160613125138.csv' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?" 我可以打开它(excel for MAC),但在我看来,CFSpreadsheet 没有创建合法的 .csv 文件,而是创建了一个 xlsx。
<cfset FileCSV = "SalesForceDailyLeads-#dateformat(getBatch.BATCH,"yyyymmdd")##timeformat(getBatch.BATCH,"HHmmss")#.csv" >
<cfset filename = "/SF/#fileCSV#">
<cfset s = spreadsheetNew() >
<cfset spreadsheetAddRow(s, "FIRST, LAST, MIDDLE, STREET, CITY, ZIP, STATE")>
<cfinclude template="SFgetList.cfm">
<cfset spreadsheetAddRows(s, getList)>
<cfspreadsheet
action="write"
overwrite = "true"
format ="csv"
name ="s"
filename ="#filename#"
>
如果我创建一个 XLS 文件,我不会像使用 CSV 文件那样遇到问题。这是代码、CFSpreadsheet 或 excel(对于 mac)的问题吗?我可以修复它吗?
使用 cffile 而不是 cfspreadsheet 创建文件。每 the documentation:
The cfspreadsheet tag writes only XLS[X] format files. To write a CSV file, put your data in a CSV formatted string variable and use the cffile tag to write the variable contents in a file.
谢谢!当然,在我发布这篇文章之后,我立即找到了文档,发现我使用不正确,所以我改用了 CFFILE。我使用了我发现的 script/function 来做到这一点。然而,将查询转换为 CSV 需要一些工作——幸运的是,其他人已经做到了。如果有人想看到它: 我在这里得到了 querytoCSV 脚本: https://gist.github.com/CreativeNotice/2775372
<cfscript>
/**
* queryToCsv
* Allows us to pass in a query object and returns that data as a CSV.
* This is a refactor of Ben Nadel's method, http://www.bennadel.com/blog/1239-Updated-Converting-A-ColdFusion-Query-To-CSV-Using-QueryToCSV-.htm
* @param {Query} q {required} The cf query object to convert. E.g. pass in: qry.execute().getResult();
* @param {Boolean} hr {required} True if we should include a header row in our CSV, defaults to TRUE
* @param {String} d {required} Delimiter to use in CSV, defaults to a comma (,)
* @return {String} CSV content
*/
public string function queryToCsv(required query q, required boolean hr = true, required string d = ","){
var colNames = listToArray( lCase(arguments.q.columnlist) );
var newLine = (chr(13) & chr(10));
var buffer = CreateObject('java','java.lang.StringBuffer').Init();
// Check if we should include a header row
if(arguments.hr){
// append our header row
buffer.append(
ArrayToList(colNames,arguments.d) & newLine
);
}
// Loop over query and build csv rows
for(var i=1; i <= arguments.q.recordcount; i=i+1){
// this individual row
var thisRow = [];
// loop over column list
for(var j=1; j <= arrayLen(colNames); j=j+1){
// create our row
thisRow[j] = replace( replace( arguments.q[colNames[j]][i],',','','all'),'""','""""','all' );
}
// Append new row to csv output
buffer.append(
JavaCast( 'string', ( ArrayToList( thisRow, arguments.d ) & iif(i < arguments.q.recordcount, "newLine","") ) )
);
}
return buffer.toString();
};
</cfscript>
<cfinclude template="getDups.cfm">
<cfinclude template="SFgetList.cfm">
<cfset FileCSV = "SalesForceDailyLeads-#dateformat(getBatch.BATCH,"yyyymmdd")##timeformat(getBatch.BATCH,"HHmmss")#.CSV" >
<cfset filename = "/mnt/nas-share/data/feed/SF/#fileCSV#">
<cfset qc = #queryToCsv(getList, false, ",")# >
<cfoutput>#qc#</cfoutput>
<cfset heads= "FIRST, LAST, MIDDLE, STREET, CITY, ZIP, STATE">
>
<cffile
action = "write"
file = #filename#
output = #heads#
addNewLine = "yes"
fixnewline = "no">
<cffile
action = "append"
file = #filename#
output = #qc#
addNewLine = "yes"
fixnewline = "no">