【问题标题】:How to format spreadsheet columns using ColdFusion?如何使用 ColdFusion 格式化电子表格列?
【发布时间】:2012-05-17 22:59:29
【问题描述】:

我正在使用 SpreadsheetFormatColumns() 将电子表格中的列格式化为“文本”,但我不知道该怎么做,livedocs 中的所有格式都是数字、货币或日期...像

SpreadsheetFormatColumns(mySpreadsheet, {dataFormat="text"}, "1-15")

在外面吗?这真是烦死我了……

谢谢

【问题讨论】:

    标签: coldfusion spreadsheet


    【解决方案1】:

    在 ColdFusion 9.0.1(即更新程序 1)中,如果您使用 SpreadsheetSetCellValue(),它将遵循您之前设置的格式。因此,要在填充工作表时强制将列作为文本,您可以使用 3 步过程:

    1. 填充电子表格,忽略错误解释的数值。
    2. 将所需列格式化为文本。
    3. 用正确的值替换列中每一行中的错误值,该值现在将被视为文本。

    这是一个示例,您可以将其复制到 .cfm 中并按原样运行(需要 CF9.0.1)

    <cfscript>
        // Create a 2 column, 2 row query. The first column contains numbers or possible numbers we want formatted as text in our spreadsheet
        q   =   QueryNew( "" );
        QueryAddColumn( q,"NumbersAsText","VarChar",[ 01050094071094340000,"743059E6" ] );
        QueryAddColumn( q,"Text","VarChar",[ "abc","def" ] );
        // Get the column names as an array so we can get at them more easily later
        columns =   q.getMetaData().getColumnLabels();
        // Create a new spreadsheet object
        sheet   =   SpreadSheetNew( "test" );
        // specify the column we want formatted as text
        forceTextColumnNumber   =   1;
        // Use the query column names as column headers in our sheet
        SpreadSheetAddRow( sheet,q.columnList );
        // Add the data: the numbers will be inserted as numeric for now
        SpreadSheetAddRows( sheet,q );
        // Now we format the column as text
        SpreadSheetFormatColumn( sheet,{ dataformat="text" },forceTextColumnNumber );
        // Having formatted the column, add the column from our query again so the values correct
        while( q.next() )
        {
            // Skip the header row by adding one
            rownumber   =   ( q.currentrow + 1 );
            // Get the value of column at the current row in the loop
            value   =   q[ columns[ forceTextColumnNumber ] ][ q.currentrow ];
            // replace the previously added numeric value which will now be treated as text
            SpreadsheetSetCellValue( sheet,value,rownumber,forceTextColumnNumber );
        }
        // Download the object as a file
        sheetAsBinary   =   SpreadSheetReadBinary( sheet );
        filename    =   "test.xls";
    </cfscript>
    <cfheader name="Content-Disposition" value="attachment; filename=#Chr(34)##filename##Chr(34)#">
    <cfcontent type="application/msexcel" variable="#sheetAsBinary#" reset="true">
    

    默认情况下,查询的第一列中的两个值都将被视为数字(第二列为十六进制)。使用此方法都将其原始值保留为文本。

    【讨论】:

    • 有关后续和更多详细信息,请参阅此博客文章:cfsimplicity.com/16/…
    • 很好的解释。虽然看起来很奇怪,但在使用查询对象(即 SpreadSheetAddRows(sheet, query) 时,格式仍然得到尊重。是时候搜索错误数据库了;)
    【解决方案2】:

    根据this chart,文本占位符使用“@”(不带引号)。

    【讨论】:

    • 谢谢,事实证明“文本”也是一种可能性(相同的输出),但是需要在填充行之后调用它们(在我看来这没有意义),因为现在而不是有'01050094071094340000'(所需的输出)我有'1.050094071094339e+18'这似乎是一个大问题(至少对我来说)使用常规的“自动”格式,它将删除所有前导零
    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 2023-03-14
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多