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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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>.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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