2010-06-20 31 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

相關問題