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,314 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,314 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,314 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,314 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,314 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
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools