Solved

CFFILE and leading zeros from SQL variables

Posted on 2012-03-28
10
408 Views
Last Modified: 2012-07-06
When I write my SQL variable (recID) to a cffile excel, I lose the leading zeros. The field is a varchar 6 characters in length.

Any suggestions?
0
Comment
Question by:traport
10 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37776901
It's happening because Excel is parsing the data as a number - not text. I'm guessing you're outputting CSV or XML, correct? I don't think there's any way to address that using this method because there's no way to tell Excel how to properly format the data.

You could try generating your Excel document using Apache POI (http://poi.apache.org/), which generates a "real" Excel document and lets you specify the cell types. Just drop the JARs into your CF class path (c:\coldfusion x\wwwroot\web-inf\lib).

Here's an example...

<cfset filename="report.xls">
<cfset out = createObject("java", "java.io.FileOutputStream").init(#filename#)>

<cfset wb = createObject("java", "org.apache.poi.hssf.usermodel.HSSFWorkbook")>
<cfset s = wb.createSheet("Test Report")>

<!--- create right-aligned column style --->
<cfset alignRightStyle = wb.createCellStyle()>
<cfset alignRightStyle.setAlignment(alignRightStyle.ALIGN_RIGHT)>

<!--- create header style--->
<cfset headerStyle = wb.createCellStyle()>
<cfset headerStyleFont = wb.createFont()>
<cfset headerStyleFont.setBoldWeight(headerStyleFont.BOLDWEIGHT_BOLD)>
<cfset headerStyle.setFont(headerStyleFont)>

<!--- create header-right style--->
<cfset headerRightStyle = wb.createCellStyle()>
<cfset headerRightStyleFont = wb.createFont()>
<cfset headerRightStyleFont.setBoldWeight(headerStyleFont.BOLDWEIGHT_BOLD)>
<cfset headerRightStyle.setFont(headerStyleFont)>
<cfset headerRightStyle.setAlignment(alignRightStyle.ALIGN_RIGHT)>

<cfset i = 0>

<cfset r = s.createRow(i)>
<cfset c = r.createCell(0)>
<cfset c.setCellValue('Header 1')>
<cfset c.setCellStyle(headerStyle)>
<cfset c.setCellType(c.CELL_TYPE_STRING)>

<cfset c = r.createCell(1)>
<cfset c.setCellValue('Header 2')>
<cfset c.setCellStyle(headerStyle)>
<cfset c.setCellType(c.CELL_TYPE_STRING)>

<cfset c = r.createCell(2)>
<cfset c.setCellValue('Header 3')>
<cfset c.setCellStyle(headerRightStyle)>
<cfset c.setCellType(c.CELL_TYPE_STRING)>

<!--- create ten rows --->
<cfloop from="1" to="10" index="i">
	<cfset r = s.createRow(i)>
	<cfset c = r.createCell(0)>
	<cfset c.setCellValue('value 1')>
	<cfset c.setCellType(c.CELL_TYPE_STRING)>
	
	<!--- blank cells --->
	<cfset c = r.createCell(1)>
	<cfset c.setCellValue('')>	
	<cfset c.setCellType(c.CELL_TYPE_STRING)>
	
	<cfset c = r.createCell(2)>
	<cfset c.setCellValue('value 3')>
	<cfset c.setCellStyle(alignRightStyle)>
	<cfset c.setCellType(c.CELL_TYPE_STRING)>
</cfloop>
		
<cfset s.createFreezePane(1, 1)>
<cfset s.setColumnWidth(0, 5000)>
<cfset s.setColumnWidth(1, 5000)>
<cfset s.setColumnWidth(2, 5000)>

<!--- print settings --->
<cfset wb.removePrintArea(0)>	
<cfset wb.setPrintArea(0,"$A$1:$C$#i#")>

<cfset h = s.getHeader()>
<cfset h.setLeft("Report")>
	
<cfset s.setAutobreaks(true)>
<cfset s.setPrintGridlines(true)>

<cfset ps = s.getPrintSetup()>
<cfset ps.setLandscape(true)>
<cfset ps.setFitWidth(1)>
<cfset ps.setFitHeight(0)>

<!--- output the sheet --->
<cfset wb.write(out)>
<cfset getPageContext().getResponse().setContentType('application/vnd.ms-excel')>
<cfset getPageContext().getResponse().setHeader('Content-Disposition', 'filename=#filename#')>
<cfcontent reset="true" file="#filename#" deletefile="false">

Open in new window


If you're on ColdFusion 9 there is also a <CFSPREADHEET> tag available, but I haven't used it...
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec17cba-7f87.html
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 37776949
If you're writing the excel file using table tag, you can include this style...

<td style='mso-number-format:000000'>#yourVal#</td>
0
 

Author Comment

by:traport
ID: 37777116
Thanks for the responses. I'd love to do it simply through the td style. That said, it's not just numeric. It can be alphanumeric... but when there are leading zeros it loses them. But that style didn't appear to work even on the numeric. Thoughts?

No, I'm not on CF9. I wish I were.

I'm under some time constraints or I would look into Apache POI. As it is, they may have to do w/o their leading zeros.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37777278
Try a slight variation of gd's suggestion. The "@" symbol should force the value to be treated as text

      <td style='mso-number-format:"\@";'>012345</td>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37777641
You could try generating your Excel document using Apache POI ... Just drop the JARs into your CF class path (c:\coldfusion x\wwwroot\web-inf\lib).

Fyi, CF7+ already include older versions of POI. You don't need to anything extra to do basic xls files. Only if you need newer features.  Bear in mind multiple versions do not always co-exist well. You may have to use the javaLoader.cfc instead or possibly replace the old version.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:traport
ID: 37777861
Coldfusion cffile doesn't like this at all: <td style='mso-number-format:"\@";'>012345</td>

I have tried double-quoting it and it's still breaking.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37777915
I wouldn't try writing complex html directly, because you'll almost always run into problems with nested quotes. Try using cfsavecontent instead.   Be sure to wrap it in <cfoutput> tags if the content contains #variables#

ie   <cfsavecontent variable="dataToWrite">
              <td style='mso-number-format:"\@";'>012345</td>
              ....
      </cfsavecontent>
      <cffile action="write" output="#dataToWrite#" ...>

That's assuming you actually need to write it to disk ie it's not for an on-the-fly download.
0
 
LVL 2

Expert Comment

by:sumCold
ID: 37796309
send single quote before your output... this will not change your output, but excel take it as string/text instead of number..

e.g.
'01234

I guess this should be your last solution if above all will not work.. :)
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 37796755
I guess this should be your last solution if above all will not work.. :)

Yep, I'd use the "\@" trick if possible, because it doesn't change the cell value. But if for some reason it doesn't, the single quote almost certainly will.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37886353
Any luck?
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 40
How to query date ranges with SQL 6 26
Report Builder 9 31
Azure SQL DB? 3 22
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now