HTML To Excel The Easy Way

27 July 2005 · Last updated: 15 October 2006

While experimenting with ASP recently I discovered it can output files as various types, such as Excel and even images. Take a look at the following example. If you save the code in a file with the extension .asp and run it from the server, it offers to download an Excel file for you.

  1. <%response.ContentType="application/vnd.ms-excel"%>
  2. <html>
  3. <head>
  4. <style type="text/css">
  5. td {padding:2em; background-color:yellow; border:1px dotted #000;}
  6. </style>
  7. </head>
  8. <body>
  9. <table>
  10. <tr>
  11. <td>1</td>
  12. <td>2</td>
  13. <td>3</td>
  14. <td>4</td>
  15. </tr>
  16. <tr>
  17. <td>=sum(a1:d1)</td>
  18. <td>6</td>
  19. <td>7</td>
  20. <td>8</td>
  21. </tr>
  22. </table>
  23. </body>
  24. </html>

I opened the resulting Excel file and found... the same code! Normally, Excel files are full of unreadable characters that you cannot decipher in a word processor. So I was very surprised to see that ASP had simply delivered the exact same code to create an Excel file. This means two things:

I started to experiment with what you could put in HTML that would also work in Excel. The above code demonstrates some of what I managed to get working. I couldn't find a way to adjust the cell height or width, except by using padding. Borders work nicely though, without the need for collapsing the table cells via CSS. Solid borders are quite thick though.

Note the formula in one cell. It works! You have to expand the cell to see the result though.

Here's another test I developed separately when having problems with fonts on my system. I loaded it into Excel without changing it, just by renaming the file from .html to .xls. Each line was correctly styled in Excel with the right font.

  1. <html>
  2. <head>
  3. <title>Font Test</title>
  4. <style type="text/css">
  5. body {font-size:170%;}
  6. .f1 {font-family:helvetica;}
  7. .f2 {font-family:arial;}
  8. .f3 {font-family:univers;}
  9. .f4 {font-family:palatino;}
  10. .f5 {font-family:'palatino linotype';}
  11. .f6 {font-family:courier;}
  12. </style>
  13. </head>
  14. <body>

  15. <p class="f1">Helvetica: This is a TEST for the font. 1234567890</p>

  16. <p class="f2">Arial: This is a TEST for the font. 1234567890</p>

  17. <p class="f3">Univers: This is a TEST for the font. 1234567890</p>

  18. <p class="f4">Palatino: This is a TEST for the font. 1234567890</p>

  19. <p class="f5">Palatino Linotype: This is a TEST for the font. 1234567890</p>

  20. <p class="f6">Courier: This is a TEST for the font. 1234567890</p>

  21. </body>
  22. </html>

This could pave the way for offering Excel versions of tables on your website. Experiment yourself with the code and see what you can achieve.

Notes:

Useful Info

EMAIL: www.designdetector.com (replace the 1st dot with "@")

NEWSFEED: Subscribe to news of fresh posts and site updates. (RSS 2.0 compatible newsreader required.)

Disclaimer

Some links on this website lead to information provided by external services not under my control, therefore I am not responsible for the content or accuracy of the linked information.

All code examples are not guaranteed 100% free from bugs and/or mistakes. Use them at your own risk. I do not take any blame should a problem occur relating to use of code on this site given as an example for your own use. Such code has been tested and found to work for me, but I cannot vouch for other computer systems (existing now, or in the future) which it may be used on, or changes you introduce yourself based on my code.

This website is © 2008 Christopher Hester, except where separate authors are named. No part of this website may be reproduced or re-used in any way without my prior permission, except content added from separate authors (who retain the copyright on their material), examples of code, and any other content I explicity state is free to copy and make use of.

This page was last updated on 15 October 2006.

What's New

Recent Finds


View previous finds


Current Reading

The Suspicions Of Mr Whicher (Or The Murder At Road Hill House) by Kate Summerscale
5%
Genesis: Chapter & Verse by Tony Banks, Phil Collins, Peter Gabriel, Steve Hackett & Mike Rutherford
70%
Digital Photographer's Handbook by Tom Ang
70%