2010-06-20 30 views
5

我使用SpreadsheetFormatColumns()将电子表格中的列格式化为“文本”,但我不知道如何做到这一点,livedoc中的所有格式都是用于数字,货币或日期的。像如何使用ColdFusion格式化电子表格列?

SpreadsheetFormatColumns(mySpreadsheet,{DATAFORMAT = “文本”}, “1-15”)

了吗?这实在是烦我......

感谢

回答

1

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

+0

谢谢,原来,“文本”也是可能的(相同的输出),但他们需要的,因为现在被称为,而不必“01050094071094340000”后,您填充行(不使在我看来意义上)(期望的输出)我有'1.050094071094339e + 18'这看起来像一个大问题(至少对我来说) 使用常规的“自动”格式,它将删除所有前导零 – raulriera 2010-06-21 00:11:55

7

在ColdFusion 9.0.1(即更新器1)中,如果使用SpreadsheetSetCellValue(),它将遵循先前设置的格式。因此,在填充工作表时强制将某一列设置为文本,您可以使用三个步骤:

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

默认情况下,该值在我的查询的第一列会被视为数字(第二个为HEX)。使用这种方法都将文本的原始值保留下来。

+1

有关后续和更多详细信息,请参阅此博客文章:http://cfsimplicity.com/16/forcing-values-to-be-inserted-into-spreadsheets-as-text – CfSimplicity 2011-06-20 11:05:13

+0

很好的解释。虽然看起来很奇怪,但在使用查询对象(例如SpreadSheetAddRows(工作表,查询))时,格式仍然*不受尊重。搜索错误数据库的时间;) – Leigh 2011-07-19 12:52:25

相关问题