CFCONTENT Excel with Empty Cells

Posted on 2012-03-12
Last Modified: 2012-03-13
Exporting data from our database to Excel using CF8 using <cfsetting showdebugoutput="false">
<cfheader name="content-disposition" value="attachment;filename=#OUTFILE2#.xls">
<cfcontent type="application/msexcel">

And then generating XML.

We have some cells that are supposed to be empty but they aren't actually when Excel is open.   Is there way around this?   I see the POI solution has a cell type blank but I can't see a way to replicate this with the process we are using.
Question by:digitalwise
  • 2
  • 2
  • 2

Expert Comment

ID: 37713021
There are a few different ways I've generated Excel content from ColdFusion 8 in the past...

1. CSV (for empty cells just leave a blank between the delimiters)
2. HTML table saved as .xls (for empty cell create an empty <TD></TD>)
2. Apache POI (this is the best Excel solution in my opinion)

Here's how I use apache POI to generate an Excel spreadsheet...

<cfsetting showdebugoutput="false">

<cfset filename="report.xls">
<cfset out = createObject("java", "").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)>
<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/')>
<cfset getPageContext().getResponse().setHeader('Content-Disposition', 'filename=#filename#')>
<cfcontent reset="true" file="#filename#" deletefile="false">

Open in new window


Author Comment

ID: 37714397
Thanks but I am aware of the methods - I am not in a position to redo the export that has lots of styles associated with it.  I just need to know if there is a way to generate truly empty cells with the XML method I am using.   <TD></TD> doesn't actually create an empty cell and I am using <cell><data></data></cell> and that doesn't work either.    It doesn't have anything in it but it isn't EMPTY.

Accepted Solution

micropc1 earned 500 total points
ID: 37714752
ah...i misunderstood the question. You need to omit the <data></data> tags. Just use <cell></cell>.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 36

Expert Comment

ID: 37714789
cf likes every variable to be a string (numbers, dates etc) and doesn't handle nulls returned from a db in a way that you'd necessarily expect (null value from numeric field returns 0 etc) so it doesn't surprise me that it's doing something similar with excel files & blanks

So I think the answer is no (at least not a simple way)- you can see it's even an issue in 9 here
LVL 36

Expert Comment

ID: 37714843
"at least not a simple way" - unless the solution by micropc1 works , then that would be one ;)

Author Closing Comment

ID: 37714921
Thank you so much - this is such a simple solution and it works perfectly!!   Wish Adobe or Microsoft would document!

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json format text only 4 75
Passing value to a stored procedure 8 90
microsoft access - xml 10 58
XSL FO - text wrapping and empty spaces 1 26
This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

25 Experts available now in Live!

Get 1:1 Help Now