SQL to Excel loses trailing zeros

流过昼夜 提交于 2019-12-13 15:24:18

问题


I have a SQL table that has columns of prices. The field type is varchar because some of the price fields must have "Market Price" as the data value. I am querying this data via ColdFusion and using cfspreadsheet to create an export:

<cfspreadsheet action="write" filename="prices.xlsx" 
       query="qTierPrices" 
       sheetname="Food Pricing" 
       overwrite="yes">

When looking at the spreadsheet, prices like 1.00 become 1 and 4.50 becomes 4.5 in the spreadsheet cells. Is there a way to fix this programmatically so the spreadsheet reflects exactly what's coming out of the table? Thanks in advance!


回答1:


Like Dan said, you can use spreadsheetFormatColumn for format an entire column in a spreadsheet.

The example below creates a spreadsheet using dummy data, and formats the price column to your specification.

<!--- Generating some dummy data --->
<cfset qData = queryNew('foo,bar,price','varchar,date,varchar')>

<cfset queryAddRow(qData)>
<cfset querySetCell(qData, 'foo', createUUID())>
<cfset querySetCell(qData, 'bar', now())>
<cfset querySetCell(qData, 'price', 'Market Price')>

<cfset queryAddRow(qData)>
<cfset querySetCell(qData, 'foo', createUUID())>
<cfset querySetCell(qData, 'bar', now())>
<cfset querySetCell(qData, 'price', '4.55')>

<cfset queryAddRow(qData)>
<cfset querySetCell(qData, 'foo', createUUID())>
<cfset querySetCell(qData, 'bar', now())>
<cfset querySetCell(qData, 'price', '1.5')>

<cfset queryAddRow(qData)>
<cfset querySetCell(qData, 'foo', createUUID())>
<cfset querySetCell(qData, 'bar', now())>
<cfset querySetCell(qData, 'price', '1')>

<cfset queryAddRow(qData)>
<cfset querySetCell(qData, 'foo', createUUID())>
<cfset querySetCell(qData, 'bar', now())>
<cfset querySetCell(qData, 'price', '7.6')>

<!--- create the spreadsheet and add a header row --->
<cfset theSheet = spreadsheetNew("Food Pricing", true)>
<cfset spreadsheetAddRow( theSheet, 'foo,bar,price' )>

<cfset spreadsheetAddRows( theSheet, qData )>

<!--- Format the third column (price) to a number with two decimal places --->
<cfset spreadSheetFormatColumn( theSheet, {dataformat='0.00'}, 3)>

<!--- Output the spreadsheet to a file --->
<cfspreadsheet action="write" filename="prices.xlsx" name="theSheet" sheetname="Food Pricing" overwrite="yes" >



回答2:


You didn't indicate which version of ColdFusion you are using. (Adobe tends to fix bugs in future releases of ColdFusion instead of the version that they're reported in.)

In order to output the decimal places, you could use ColdFusion's built-in SpreadSheetFormatColumn(), but you'll soon find that it doesn't work as advertised (ie, dates). Using Ben Nadel's POIUtility.cfc will provide better and more consistent results (and smaller Excel file sizes).

https://github.com/bennadel/POIUtility.cfc

I wrote an CustomTag that uses POIUtility.cfc to offer simpler exports (ie, less templating).

http://gamesover2600.tumblr.com/post/116662989199/coldfusion-cfexcelpoi

As a final last option, you could cast the columns as "text" and then loop through and format the value as desired prior to using CFSpreadsheet, but Excel may still usurp the formatting.

http://www.cflib.org/udf/castQueryColumn

If you format values as "text", the Excel cell values will be left-aligned instead of right-aligned as numeric values. In this case, you'll also need to apply "right" alignment styles to the column (or individually to each cell using SpreadsheetFormatCell, but that takes longer.)

format1=StructNew(); 
format1.alignment="right";
SpreadsheetFormatColumn(spreadsheetObj, format1, column)

In my opinion, it's best to use POIUtility since it produces correct & consistent results in ColdFusion 7 - 11, but mileage will vary if you are returning numeric values as text. (Definitely use POI for speed, date formatting & smaller file sizes.)



来源:https://stackoverflow.com/questions/31834337/sql-to-excel-loses-trailing-zeros

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