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>.
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML response optional elements 12 58
Need a modeling tool 2 41
C# XML Get Values 4 35
Are URL parameters returned from <cfhttp>? 1 11
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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