QueryNew()CF9中的数据类型

时间:2014-09-10 18:15:40

标签: types coldfusion coldfusion-9

我已经接管了运行CF9.0.1的生产服务器上的系统,我在开发人员版本中找不到该副本,所以我正在运行CF10。

我将数据从数据库导出到Excel。由于数据来自多个数据源,因此将结果手动输入到查询中,然后用于输出到Excel。我要解决的第一个问题之一就是,因为Excel自动输入数据,所以发生了奇怪的事情,比如尾随零被删除,数字变成了日期等等。经过大量研究,我尝试指定数据的数据类型进入查询为“varchar”,以便Excel将其作为文本读取。为此,我用以下代码行替换了原始的QueryNew。

dataQuery = QueryNew("row_number,function,nomenclature,hw,crit,load,sw,media,svd,bds,ecp,install,notes", "VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar, VarChar");

这对CF10很有用。然后,它通过CF9发布到生产,它没有解决任何问题。 Excel仍然没有以文本类型接收数据并自动对其进行自动格式化。所以,我尝试了以下内容。

dataQuery = QueryNew("row_number,function,nomenclature,hw,crit,load,sw,media,svd,bds,ecp,install,notes", "CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_VARCHAR");

同样,它在CF9上不起作用,但在CF10上很棒。

我缺少哪些关于CF9的东西让这个不起作用?任何帮助都会很精彩!

很抱歉没有尽快这样做。这是一个示例代码,它准确地显示了我遇到的问题。这在CF10上完美地导出到Excel,但在CF9上有问题。

<cfscript>
    dataQuery = QueryNew("row_number,function,nomenclature,hw,crit,load,sw,media,svd,bds,ecp,install,notes", "VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar");


    //Row #1
    newRow = queryaddRow(dataQuery);
    querySetCell(dataQuery,"row_number","1");
    querySetCell(dataQuery,"function","Function 1");
    querySetCell(dataQuery,"nomenclature","Nomen 1");
    querySetCell(dataQuery,"hw","185019-001"); //Sometimes axports as an exponent
    querySetCell(dataQuery,"crit","2");
    querySetCell(dataQuery,"load","Load 12B RL");
    querySetCell(dataQuery,"sw","0.0620"); //This one get the trailing 0 left off
    querySetCell(dataQuery,"media","Media 1");
    querySetCell(dataQuery,"svd","6529-02"); // Sometimes turned into a date
    querySetCell(dataQuery,"bds","BDS 1");
    querySetCell(dataQuery,"ecp","ECP1");
    querySetCell(dataQuery,"install","Install 1");
    querySetCell(dataQuery,"notes","Note1");

    //Row #2
    newRow = queryaddRow(dataQuery);
    querySetCell(dataQuery,"row_number","2");
    querySetCell(dataQuery,"function","Function 2");
    querySetCell(dataQuery,"nomenclature","Nomen 2");
    querySetCell(dataQuery,"hw","185019-005"); //Sometimes axports as an exponent
    querySetCell(dataQuery,"crit","2");
    querySetCell(dataQuery,"load","Load 12B RL");
    querySetCell(dataQuery,"sw","0.06200"); //This one get the trailing 0 left off
    querySetCell(dataQuery,"media","Media 2");
    querySetCell(dataQuery,"svd","6529-03"); // Sometimes turned into a date
    querySetCell(dataQuery,"bds","BDS 2");
    querySetCell(dataQuery,"ecp","ECP 2");
    querySetCell(dataQuery,"install","Install 2");
    querySetCell(dataQuery,"notes","Note2");

    sheet= spreadSheetNew("New", "true");

    spreadsheetAddRows(sheet,dataQuery);

    </cfscript>
<cfspreadsheet action="write" filename="c:/CF9ExcelTest.xlsx"  name="sheet" overwrite="true" > 

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

请参阅this previous question了解适用于CF9的方法:

  1. 从您的查询中填充工作表(值将不正确)
  2. 在有问题的单元格上设置正确的格式(不正确的值不会改变)然后
  3. 循环查询并使用SpreadsheetSetCellValue()重新插入有问题的值(它们现在的格式正确)
  4. http://cfsimplicity.com/16/forcing-values-to-be-inserted-into-spreadsheets-as-text

    提供了更多详细信息和代码,以简化此操作

答案 1 :(得分:1)

首先尝试将列/单元格格式化为文本。请参阅Enhancement in ColdFusion 9.0.1下文档中的格式示例。即

// Format an individual cell  ...
SpreadsheetFormatCell(sheet, {dataformat="@"}, rowNum, columnNum); 
// Format columns 4 and 7
SpreadsheetFormatColumns(sheet, {dataformat="@"}, "4,7")

不幸的是,CF9.x中的某些电子表格功能有点古怪,所以我不确定它是否可以与SpreadsheetAddRows结合使用。如果没有,您可能需要求助于循环并在其格式中并单独分配单元格值:

   ...
   SpreadsheetFormatCell(sheet, {dataformat="@"}, rowNum, columnNum); 
   SpreadSheetSetCellValue(sheet, "some value", rowNum, columnNum);
   ...

答案 2 :(得分:1)

如果Leigh的选项失败,您可以尝试以不同的方式生成Excel文件。您可以创建一个xml文件并将其保存为excel。这样,您可以为每个单元格或列设置值和数据类型。下面只是一个非常简单的例子。

<cfsavecontent variable="test">
<?xml version="1.0"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<ss:Worksheet ss:Name="Sheet1">
    <ss:Table>
        <ss:Column ss:Width="80"/>
        <ss:Column ss:Width="80"/>
        <ss:Column ss:Width="80"/>
        <ss:Row>
            <ss:Cell>
                <ss:Data ss:Type="String">185019-001</ss:Data>
            </ss:Cell>
            <ss:Cell>
                <ss:Data ss:Type="String">0.06200</ss:Data>
            </ss:Cell>
            <ss:Cell>
                <ss:Data ss:Type="String">6529-02</ss:Data>
            </ss:Cell>
        </ss:Row>
    </ss:Table>
</ss:Worksheet>
</ss:Workbook>
</cfsavecontent>

<cffile action="write" file="c:/CF9ExcelTest.xls"  output ="#test#">