1

我创建了一个冷融合页面,将 MYSQL 中的客户列表输出到 CSV 文件中,以便轻松上传到 SalesForce.com

我可以生成包含所有正确信息的文件。但是,当我尝试使用 excel 打开它时出现错误:“'SalesForceDailyLeads-20160613125138.csv' 的文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非你相信它的来源,否则不要'不要打开它。你还是要打开它吗?我可以打开它(MAC 的 Excel),但在我看来,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)的问题吗?我可以修复它吗?

4

2 回答 2

3

使用 cffile 而不是 cfspreadsheet 创建文件。根据文档

cfspreadsheet 标签只写入 XLS[X] 格式文件。要写入 CSV 文件,请将数据放入 CSV 格式的字符串变量中,并使用 cffile 标签将变量内容写入文件。

于 2016-06-13T19:32:58.397 回答
0

谢谢!当然,在我发布这个之后,我找到了文档,发现我使用不正确,我使用了 CFFILE。我使用了一个我发现的脚本/函数来执行此操作。然而,将查询转换为 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">
于 2016-06-15T14:31:12.863 回答