【问题标题】:Cold Fusion 2018: how to save .xlsx file as .csvCold Fusion 2018:如何将 .xlsx 文件另存为 .csv
【发布时间】:2021-05-14 23:56:48
【问题描述】:

Win2019 上的 CF2018 尝试将用户上传的 .xlsx 文件另存为 .csv 文件以处理到 Oracle 数据库中。 运行这段代码,完成没有错误,但结果不清晰:

<cfspreadsheet action = "read"
                format="csv"
                src="c:\bin\Nov_sales.xlsx"
                name="foo"
                > 

<cfspreadsheet action="write" 
                filename='c:\bin\Nov_sales.csv' 
                format="csv" 
                name="foo"  
                overwrite=true>

.csv 文件中的结果如下所示:

504b 0304 1400 0808 0800 d260 8f51 0000
0000 0000 0000 0000 0000 0b00 0000 5f72
656c 732f 2e72 656c 73ad 92c1 4a03 3110
865f 25cc bd9b 6d05 1169 da8b 08bd 89d4
.....

我错过了什么???

【问题讨论】:

  • cfspreadsheet 不能用于创建您想要的 csv 文件。你可能需要做这样的事情bennadel.com/blog/…
  • 你的excel文件有表头记录吗?如果是这样,您应该做的是读取 excel 文件并将其放入查询对象中。从那里,您可以轻松地将查询导出到 csv 中。 &lt;cfspreadsheet action = "read" src="c:\bin\Nov_sales.xlsx" query="qryExcel"&gt;

标签: coldfusion cfspreadsheet


【解决方案1】:

“cfspreadsheet 标签始终将电子表格数据写入 XLS 文件。要将 HTML 变量或 CSV 变量写入 HTML 或 CSV 文件,请使用 cffile 标签。”

https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-r-s/cfspreadsheet.html

或者换句话说,当使用cfspreadsheet action="write" 时,format 属性没有指定输出格式,它指定了输入格式——因为它可以是 CF 电子表格对象、查询、CSV 中的任何一个字符串或 HTML 字符串。

您在 .csv 文件中看到原始字节,因为它实际上是一个 Excel 文件,而不管 .csv 文件扩展名如何。

【讨论】:

    【解决方案2】:

    假设你的 excel 文件有一个标题记录,你应该首先将 excel 文件读入查询对象。然后,您可以从那里使用&lt;cfoutput&gt; 循环写入文件,将查询对象导出到 csv。

    <cfspreadsheet action="read" src="c:\bin\Nov_sales.xlsx" query="qryExcel">
    
    <cfoutput query="qryExcel">
        <cfset line = "#col1#,#col2#,#col3#,#col4#,#col5#">
        <cffile action="append" file="c:\bin\Nov_sales.csv" output="#line#">
    </cfoutput>
    

    【讨论】:

      【解决方案3】:

      看完后:

      “cfspreadsheet 标记始终将电子表格数据写入 XLS 文件。要将 HTML 变量或 CSV 变量写入 HTML 或 CSV 文件,请使用 cffile 标记。”

      我让它工作了,像这样:

      <cfquery name="ExportData" datasource="yourdatasource">
      
          /* Only specify the columns you need to display in the query */
      
          SELECT *    
          FROM instructor_student_session 
      
          </cfquery>
      <cfset doc_nm = "instructor_student">
      <cfscript>
              //Use an absolute path for the files.
              theDir="c:\bin\";
              theXLSXFile="#doc_nm#.xlsx";
              //Create two empty ColdFusion spreadsheet objects.
              theSheet = SpreadsheetNew(true); // creates as xlsx
              //Populate each object with a query.
              //SpreadsheetAddRows(theSheet,ExportData); //no headers
              SpreadsheetAddrows(theSheet,ExportData,1,1,true,[""],true); // add headers
          </cfscript>
      
      <cfset theCSVFile = "#doc_nm#.csv">
      
      <cfspreadsheet action="write" filename="#theDir##theXLSXFile#" name="theSheet" sheetname="Students" overwrite="true">
      
      <cfspreadsheet action = "read" src="#theDir##theXLSXFile#" format="csv" name="csvdata" sheet="1">
      
      <cffile action="write" file="#theDir##theCSVFile#" output="#csvdata#">
      
      <cfheader name="Content-disposition" value="attachment;filename=#theCSVFile#">
      
      <cfcontent type="application/vnd.ms-excel" file="#theDir##theCSVFile#" deletefile="yes">
      

      【讨论】:

        猜你喜欢
        • 2019-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2015-07-04
        • 1970-01-01
        相关资源
        最近更新 更多