Maintaining Line Breaks In An HTML-Excel File

Posted December 19, 2007 at 2:24 PM by Ben Nadel

Tags: ColdFusion

Just a minor thing that I learned yesterday. As you might know, creating a pseudo Excel document is really easy using HTML. However, when you start to play around with idea, you will quickly notice that standard break tags (<br />) will create a new cell in the next row rather than creating a line break within the current cell. To get around this, all you have to do is add some Microsoft proprietary CSS to the BR tag:

  • <br style="mso-data-placement:same-cell;" />

Now, just to see a quick little example in action:

  • <!--- Store Excel-HTML output. --->
  • <cfsavecontent variable="strData">
  •  
  • <table border=".5pt">
  • <thead>
  • <tr>
  • <th>
  • Degree
  • </th>
  • <th>
  • Year
  • </th>
  • </tr>
  • </thead>
  • <tbody>
  • <tr valign="top">
  • <td>
  • Highschool Diploma
  • <br style="mso-data-placement:same-cell;" />
  •  
  • Hackley School
  • </td>
  • <td>
  • 1998
  • </td>
  • </tr>
  • <tr valign="top">
  • <td>
  • Bachelor of Science in Computer Science
  • <br style="mso-data-placement:same-cell;" />
  •  
  • Tufts University
  • </td>
  • <td>
  • 2002
  • </td>
  • </tr>
  • </tbody>
  • </table>
  •  
  • </cfsavecontent>
  •  
  •  
  • <!--- Set header for file attachment. --->
  • <cfheader
  • name="content-disposition"
  • value="attachment; filename=data.xls"
  • />
  •  
  • <!--- Stream the content. --->
  • <cfcontent
  • type="application/excel"
  • variable="#ToBinary( ToBase64( strData ) )#"
  • />

Running this code, we immediately get prompted to save this Excel file:


 
 
 

 
HTML-Excel File With Line Breaks In Content  
 
 
 

Notice that the line breaks within the first column did not create new cells. Again, a minor note but hugely useful when you want to whip together a quick little pseudo Excel file using HTML.




Reader Comments

Dec 19, 2007 at 3:08 PM // reply »
3 Comments

Hi Ben, sort of off topic, but I was wondering if you can add some insight. I'm using your POIUtility to read an excel file I get from a customer, and the header takes up the top 7 rows (the column headers start at row 8). The HasHeaderRow feature only ignores the top row. Any way I can get it to ignore the top 8 and start reading at row 9?


Dec 19, 2007 at 4:27 PM // reply »
211 Comments

I clicked the wrong link! Sorry, I meant to report a typo to you, not "Ask Ben." The typo is "is really easing using HTML." < Really EASY.


Dec 20, 2007 at 8:05 AM // reply »
11,238 Comments

@Todd,

No worries man. Thanks for the catch. It has been fixed.


Dec 21, 2007 at 12:54 AM // reply »
18 Comments

That's great to know :)

the problem with the pseudo html xls files is that excel chokes when the file gets too big

the way around that is then to wack your data into a database and then suck it in via odbc into excel, but use '~' or '|' as your new line character and then do a search a replace on that character and type alt-0010 and the replacement character and click replace all

long winded but it works....


Dec 21, 2007 at 7:23 AM // reply »
11,238 Comments

@Zac,

Also a good solution to the problem. For small, ad-hoc reports, however, HTML is a really quick and easy solution - I just don't want to down play that fact.


Dec 27, 2007 at 9:42 AM // reply »
3 Comments

Ben,

I'm working on some data export tools that use use the HTML-Excel shortcut. Is there somewhere that you know of that has a good reference for these Microsoft pseudo classes?

Thanks,
Daniel Shaw


Dec 28, 2007 at 8:43 AM // reply »
11,238 Comments

@Daniel,

I am not aware of any solid resource. I found this tip by doing Googling around for the specific problem. One way that I found to learn about the formatting (at least in Word documents) is to do a File > Save As > Web Page, and then look at the resultant page source.


Dec 29, 2007 at 10:51 AM // reply »
3 Comments

@Ben - Excellent suggestion. Thanks.


Mar 9, 2008 at 3:05 AM // reply »
14 Comments

man oh man, the time you saved me posting this - exactly what I needed for exactly what I am working on right now, and Google found it for me right away. Great job ... thanks so much!


Apr 9, 2008 at 12:55 PM // reply »
1 Comments

Great post!

I understood the HTML-Excel-Line feed problem, but I couldn't figure out the solution. I'll never get back the last 4 hours of futility, but, thanks to your post, I won't have struggle with this again.

Thanks!


Apr 9, 2008 at 12:58 PM // reply »
11,238 Comments

@Mrando,

Glad to have helped.


Aug 15, 2008 at 5:04 PM // reply »
1 Comments

Thanks a lot! First hit and could never have figured it out myself.


Jun 3, 2009 at 7:33 PM // reply »
1 Comments

This is a big help! Been trying for hours to figure out why I was getting so many extra rows... Much appreciated.


Rob
Apr 20, 2010 at 3:14 PM // reply »
1 Comments

Thank you so much! This is exactly what I needed!!!


Apr 21, 2010 at 11:44 AM // reply »
1 Comments

Neat, but there's a problem: Yes this CSS does allow line breaks in a single cell, but on the other hand excel still uses the full width of the unbroken text to calculate the column width. So it's really an incomplete fix (since the reason to use line breaks in a single cell would almost always be to conserve horizontal space)... Would love to see a solution that also resizes the column appropriately.


Apr 24, 2010 at 1:40 PM // reply »
1 Comments

I'm running into the same issues as Charlottesville Media Group mentioned. Trying to figure out how to get excel to shrink the column width to fit the wrapped data. I can double click on the column divider after it's open in excel then it is shrunk the the data width. I wish Excel would auto size it this way when opening.

If you are looking for a reference for ALL that you can do... Google "Microsoft Office HTML and XML Reference" You will find a downloadable help file from Microsoft that tells you everything you can do. It is a bit overwhelming how much control you actually have! I just haven't found in that document how to "auto size" the columns the way I want!


Aug 8, 2010 at 9:03 PM // reply »
11,238 Comments

@Charlottesville, @Silly,

Hmmm, that must have changed in new versions of Excel. If you look at my screen shot, the column only takes the width of the longest single line of text. My whole system just crashed last week and I actually switched to Mac, so I don't even have an Excel file to test with.


Dec 12, 2010 at 12:16 PM // reply »
1 Comments

That's simply great! Exactly what I was looking for. Thank you so much!


Feb 10, 2011 at 5:54 PM // reply »
1 Comments

Battled with this for a few hours. Couldn't get it to work on my Mac. Moved to my VM and it worked great.


Apr 2, 2012 at 10:53 PM // reply »
1 Comments

Thanks a lot for this. Spent 3+ hrs trying to get this to work. Using it with BIRT reporting tool and Java. Thanks!!!


Apr 15, 2012 at 11:29 AM // reply »
1 Comments

Thx a lot!!! I have built some views in lotus notes to open directly in excel and was getting crazy with this "problem"! thx again!
Alex


Apr 26, 2012 at 1:07 PM // reply »
1 Comments

I was looking all over for this answer until I came across your post. This was a HUGE help, thanks!


Oct 3, 2012 at 9:26 AM // reply »
6 Comments

Thanks Ben for this tip!


rwe
Mar 7, 2013 at 5:04 AM // reply »
1 Comments

thanks a lot. fix my problem



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools