Tag Archives: programming

cfspreadsheet + GetTempFile() = NullPointerException

Every once in a while I encounter a problem with something so blindingly simple that

  1. I can’t imagine I haven’t tried it before, and
  2. I can’t believe no one else has documented the problem either.

ColdFusion 9 and later has a tag called <cfspreadsheet> which makes it dead simple to read, update, or create an Excel file. Want to plug the results of a query into a spreadsheet? Literally one tag with just three parameters.

There’s also function GetTempFile() that will create a guaranteed unique file on the server that you can then manipulate.

The obvious solution if you want to create a spreadsheet to download is to create a temp file, write to it with cfspreadsheet, then serve it up.

<cfset filename=GetTempFile(GetTempDirectory()) >
<cfspreadsheet action="write" overwrite="true" filename="#filename#" query="myquery">
<cfheader name="Content-disposition" value="attachment;filename=MyFile.xls">
<cfcontent type="application/msexcel" file="#filename#">
<cffile action="delete" file="#filename#">

It turns out this won’t work. You’ll get a Null Pointer Exception in the logs. Not because of anything you’d expect, though…it turns out that cfspreadsheet is relying on the file extension, and since GetTempFile() gives you a file ending in .tmp instead of .xls, it isn’t sure what to do with the file.

Solution: Find some other way to name your temporary file, and give it an .xls extension.

Recent Links: Recalls, Ticket Meltdowns and More

All right, all right! ) Happy now?

Links: Plugs, Top Websites, Leverage, Hackers & Doomsdays

Fake Science diagrams the Types of Plugs. I’m going to have to hang on to this one for the “shameless” variety.

Nmap has a nice visualization of the Internet’s top websites: Icons of the Web

Leverage has put up a set of short video clips from their Comic-Con presentation. Let’s go steal a Comic-Con. (Interestingly enough, they picked some of the same bits I did for my own write-up of the event.)

Hilarious. People working on viruses are actually sending their crash reports to Microsoft — including the malicious code!

The Bad Astronomer presents: Top 5 Ways the Universe Could Wipe Out Humankind. He goes into these threats (some likely, some unlikely, and some certain — but not for millions of years) in a lot more detail in his book, Death from the Skies!, which is a fascinating read.