Solved

CFCONTENT Excel with Empty Cells

Posted on 2012-03-12
6
681 Views
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.
0
Comment
Question by:digitalwise
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:micropc1
Comment Utility
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)>
</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

0
 

Author Comment

by:digitalwise
Comment Utility
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.
0
 
LVL 7

Accepted Solution

by:
micropc1 earned 500 total points
Comment Utility
ah...i misunderstood the question. You need to omit the <data></data> tags. Just use <cell></cell>.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 36

Expert Comment

by:SidFishes
Comment Utility
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
0
 
LVL 36

Expert Comment

by:SidFishes
Comment Utility
"at least not a simple way" - unless the solution by micropc1 works , then that would be one ;)
0
 

Author Closing Comment

by:digitalwise
Comment Utility
Thank you so much - this is such a simple solution and it works perfectly!!   Wish Adobe or Microsoft would document!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

16 Experts available now in Live!

Get 1:1 Help Now