【问题标题】:CSV file to Excel 2010 formats datetime fields to timeCSV 文件到 Excel 2010 将日期时间字段格式化为时间
【发布时间】:2013-05-14 20:04:03
【问题描述】:

我在 SQL Server 中有几个日期时间字段,它们以 .txt 或 .csv 格式输出到 CSV 文件中。在 Excel 2010 中自动打开 .csv 文件时,它会将日期时间格式化为 mm:ss.0 并完全删除日期部分。

我读过这个:Opening a CSV file in excel changes my formats 和这个:Date format that is guaranteed to be recognized by Excel

这是存储在 CSV 文件 (.txt) 中的原始文本的 sn-p。请注意,日期部分是 ISO 格式。时间部分确实有毫秒。

"EVENTSTARTDATE","EVENTTITLE","PURCHASEDATE"
"2013-04-17 00:00:00.0","Test Event","2013-04-17 15:06:27.56"

这是 Excel 中的格式:

EVENTSTARTDATE  EVENTTITLE  PURCHASEDATE
00:00.0         Test Event  06:27.6

点击字段显示这些值:

EVENTSTARTDATE                      PURCHASEDATE
4/17/2013  12:00:00 AM              4/17/2013  3:06:28 PM

当我转到“格式化单元格...”并查看字段格式时,它是“自定义”和“mm:ss.0”。我可以将其重新格式化为 Date 格式并且它工作正常,所以显然数据本身是正确的。为什么 Excel 只格式化时间部分,为什么要去掉小时数?如果字段类型为“常规”,Excel不应该能够解析日期时间数据吗?

其他可能相关的信息: 我正在使用 ColdFusion 9 并且有使用 CreateODBCDateTime() 函数的代码。

<cfif isDate(raw)>
   <cfset raw = CreateODBCDateTime(raw)>
</cfif>

【问题讨论】:

  • 我将研究 CF9 中引入的 cfspreadsheet 标签:help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/…
  • 仅供参考:在以后的问题中,您可能还需要包含主要的coldfusion 标签以吸引更广泛的观众:)
  • 感谢@Leigh 的建议,我会这样做的。

标签: excel ms-access datetime csv coldfusion-9


【解决方案1】:

解决了这个问题。该函数的代码中隐藏了一个函数调用:

<cffunction name="QueryToCSV" access="public" returntype="string" output="false" hint="Converts a query to a comma separated value string.">
        <cfargument name="Query" type="query" required="true" hint="the query being converted to CSV">
        <cfargument name="Headers" type="string" required="false" default="#arguments.query.columnList#" hint="the list of field headings to be used when creating the CSV value">
        <cfargument name="Fields" type="string" required="false" default="#arguments.query.columnList#" hint="the list of query fields to be used when creating the CSV value">
        <cfargument name="lstDateTimeFields" type="string" required="false" default="" hint="the list of fields that should be output in a date/time format">
        <cfargument name="CreateHeaderRow" type="boolean" required="false" default="true" hint="flags whether or not to create a row of header values">
        <cfargument name="Delimiter" type="string" required="false" default="," hint="the field delimiter in the CSV value">

        <!--- 
            Author:
            Ben Nadel 

            Link:
            http://www.bennadel.com/blog/1239-Updated-Converting-A-ColdFusion-Query-To-CSV-Using-QueryToCSV-.htm
        --->
...
</cffunction>

这就是它用来将查询转换为 CSV 的方法。使用正确的字段名称设置 lstDateTimeFields 参数会导致它们在 Excel 文件中正确格式化。

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多