2016-08-22 270 views
1

我使用cfspeadsheet将查询导出到Excel文件。它正在工作并创建Excel工作表。但是,问题是其中一列(即card_number)包含一个15位数字,如下所示:4.5421E+15。有没有一种方法可以显示完整的号码:4254218068670980如何在Excel工作表中显示完整号码?

<!--- create manual query for demo ---> 
<cfset qData = queryNew("")> 
<cfset queryAddColumn(qData, "NumericCol", "BigInt",["4254218068670980"])> 
<cfset queryAddColumn(qData, "StringCol", "Varchar",["4254218068670980"])> 
<cfset queryAddColumn(qData, "DecimalCol", "Decimal",["4254218068670980"])> 

<!--- export to file ---> 
<cfspreadsheet action="write" 
     filename="c:/path/to/myFile.xls" 
     query="qData" 
     overwrite="true"> 
+0

我真的希望这不是一个信用卡号码.... ;-) – Leigh

+0

nope ramdom号码,我可以改变它的原因任何混淆 –

回答

1

您需要定义和使用单元格格式来显示完整的数字。以下是您的代码示例代码片段:

<cfscript> 
theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "new_data.xls"; 
//Create a new Excel spreadsheet object. 
theSheet = SpreadsheetNew("Expenses"); 
//Set the value a cell. 
SpreadsheetSetCellValue(theSheet,"4254218068670980",1,4); 
//Set value into another cell. 
SpreadsheetSetCellValue(theSheet,"4254218068670980",2,4); 
// Define a format class for for number. 
longNum=StructNew(); 
longNum.dataformat = "0"; 
//Now use this class to format cell 
SpreadsheetFormatCell(theSheet,longNum,2,4); 
</cfscript> 

有许多支持的格式可用;要查看完整列表,您可以查看here。 此外,就像SpreadsheetFormatCell你可能想要使用SpreadsheetFormatColumn或其他相关的功能。

+0

在填充数据之前,您需要格式化单元格。我还注意到,可以用这种方式格式化的单元格和行的数量限制为4000,但我还没有找到解决方法。 这就是为什么我们使用Ben Nadel的POI自定义标签,你可以在这里找到(https://github.com/bennadel/POIUtility.cfc)。 – Sander

+1

@Sander - 最有可能是单独格式化单元格造成的。 [Excel限制了可以应用的“样式”数量](http://stackoverflow.com/questions/37817997/spreadsheetformatrow-abruptly-stops-working/37820034#37820034)。 .xls的限制低于.xlsx。通常解决方案是将格式应用一次 - 对范围或整列 - 而不是单独应用。 – Leigh

0

(太长评论...)

FWIW,CFSpreadsheet是专为非常简单的出口,没有很多花俏的。如果您需要特殊格式,则必须改用电子表格功能。

与您当前的代码最接近的相当于SpreadsheetAddRows(sheet, query)函数。它使用提供的查询对象中的数据填充工作表。如Viv's answer所述,您可以根据需要格式化列。例如,如果你想为文本,待处理的值,使用{dataformat = "@"}

<cfscript> 
    SpreadsheetAddRows(theSheet, qData); 
    SpreadsheetFormatColumns(theSheet, {dataformat = "@"}, "1-3"); 
    SpreadSheetWrite(theSheet, "c:/path/to/myFile.xls", true); 
</cfscript> 

顺便说一句,在文档中的例子并不总是最好的或最干净的。考虑他们的起点,而不是完全按照“原样”使用代码。