CFCONTENT Excel with Empty Cells

Posted on 2012-03-12
Medium Priority
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", "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)>
<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


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 2000 total points
ID: 37714752
ah...i misunderstood the question. You need to omit the <data></data> tags. Just use <cell></cell>.
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

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) http://docs.transfer-orm.com/wiki/Handling_Null_Values.cfm 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 http://cfsearching.blogspot.com/2010/05/cf9-question-do-spreadsheet-functions.html
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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 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