QueryNew() Data Types in CF9

元气小坏坏 提交于 2020-01-24 15:08:14

问题


I have taken over a system that is on a production server that is running CF9.0.1 and I cannot find a copy of that in the developer's edition, so I am running CF10.

I am exporting data from the database to Excel. Because the data comes from multiple datasources, the results are manually entered into a query that is then used to output to Excel. One of my first problems to solve was that, because Excel automatically types data, wierd things were happening like trailing zeros being dropped, numbers turned into dates, etc. After a lot of research, I tried specifying the datatypes of the data going into the query as "varchar" so that Excel would read it as text. To do this, I replaced the original QueryNew with the following line of code.

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");

That worked great on CF10. Then, it got posted to production with CF9 and it didn't solve anything. Excel is still not receiving the data as a text type and is autoformatting it. So, I tried the following instead.

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");

Again, it didn't work on CF9, but is great on CF10.

Is there something about CF9 that I am missing that is making this not work? Any help would be wonderful!

Sorry for not doing this sooner. This is sample code that exactly shows the problem I'm having. This exports to Excel perfectly on CF10, but has problems on 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" > 

Thank you for any help.


回答1:


Try formatting the column/cells as text first. See the format example in the docs, under Enhancement in ColdFusion 9.0.1. ie

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

Unfortunately some of the spreadsheet functions in CF9.x were a bit quirky, so I am not sure if that will work in combination with SpreadsheetAddRows. If not, you may need to resort to a loop and within it format and assign the cell values individually:

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



回答2:


See this previous question for an approach that should work in CF9:

  1. populate the sheet from your query (the values will be incorrect)
  2. set the correct formatting on the problematic cells (the incorrect values won't change) then
  3. loop over the query and re-insert the problematic values using SpreadsheetSetCellValue() (they will now be in the correct format)

More detail and code to make this easier is available at http://cfsimplicity.com/16/forcing-values-to-be-inserted-into-spreadsheets-as-text




回答3:


If Leigh's option fails you could try generating the Excel file in a different way. You can create an xml file and save it for excel. This way you can set the value and data type for each cell or column. Below just a very simple example.

<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#"> 


来源:https://stackoverflow.com/questions/25772326/querynew-data-types-in-cf9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!