Click here to Skip to main content
6,291,124 members and growing! (15,768 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

100% Easy, 100% Free, 100% Best Way Of Producing Simple Excel Spreadsheets From The Web

By alex turner

If you have a php, asp, ruby or whatever based website, it is usually a pain to get simple but good looking Excel spreadsheet out of the site. This solution is too simple to be true (but it is).
Javascript, Windows, .NET, ASP.NET, IIS, Visual Studio, WebForms, Dev
Posted:19 Feb 2007
Updated:1 Mar 2007
Views:16,879
Bookmarked:14 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 2.45 Rating: 2.89 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
1 vote, 14.3%
3
1 vote, 14.3%
4
3 votes, 42.9%
5

If you have a php, asp, ruby or whatever based website, it is usually a pain to get simple but good looking Excel spreadsheet out of the site. This solution is too simple to be true (but it is).

I cannot lay claim to having found this out, it was my good friend Rudolph Wijburg.

Basically, Microsoft put in a cool feature (the ability to read HTML out of xls file) in Excel but forgot to tell anyone!

. So, you create a nice looking table as standard HTML, but give it a file name ending with .xls and a mime type of 'application/excel' and away you go.

Here is a set of worked examples: Excel HTML 1 The simplest example has just a raw HTML table. Excel recognises the difference between TH and TD and give TH cells a bold font. The HTML for the above example is:

<html>
<body>
<table>
<tr><th>Col A</th><th>Col B</th><th>Col C</th></tr>
<tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>Val A2</td><td>Val B2</td><td>Val C2</td></tr>
<tr><td>Val A3</td><td>Val B3</td><td>Val C3</td></tr>
</table>
</body>
</html>

Excel does not seem to respond to styling, but it does recognise the 'old fashioned' table style HTML attributes like 'border': Excel HTML 2

<html>
<body>
<table border=1>
<tr><th>Col A</th><th>Col B</th><th>Col C</th></tr>
<tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>Val A2</td><td>Val B2</td><td>Val C2</td></tr>
<tr><td>Val A3</td><td>Val B3</td><td>Val C3</td></tr>
</table>
</body>
</html>

Individual cells can be given different background using the 'bgcolor' attribute: Excel HTML 3

<html>
<body>
<table border=1>
<tr><th bgcolor='#AAAAAA'>Col A</th><th bgcolor='#AAAAAA'>Col B</th><th bgcolor='#AAAAAA'>Col C</th></tr>
<tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>Val A2</td><td>Val B2</td><td>Val C2</td></tr>
<tr><td>Val A3</td><td>Val B3</td><td>Val C3</td></tr>
</table>
</body>
</html>

You have some control over the font color and size using the font tag: Excel HTML 4

<html>
<body>
<table border=1>
<tr><th bgcolor='#AAAAAA'>Col A</th><th bgcolor='#AAAAAA'>Col B</th><th bgcolor='#AAAAAA'>Col C</th></tr>
<tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>Val A2</td><td>Val B2</td><td>Val C2</td></tr>
<tr><td>Val A3</td><td>Val B3</td><td><font color='#FF0000'>Val C3</font></td></tr>
</table>
</body>
</html>

You can also mess with the font using the traditional i,u,b etc tags: Excel HTML 5

<html>
<body>
<table border=1>
<tr><th bgcolor='#AAAAAA'>Col A</th><th bgcolor='#AAAAAA'>Col B</th><th bgcolor='#AAAAAA'>Col C</th></tr>
<tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>Val A2</td><td>Val B2</td><td>Val C2</td></tr>
<tr><td>Val A3</td><td>Val B3</td><td><font color='#FF0000'><b><i><u>Val C3</u></i></b></font></td></tr>
</table>
</body>
</html>

You can also even embed links using the A tag! Excel HTML 6

<html>
<body>
<table border=1>
<tr><th bgcolor='#AAAAAA'>Col A</th><th bgcolor='#AAAAAA'>Col B</th><th bgcolor='#AAAAAA'>Col C</th></tr>
<tr><td><a href='http://nerds-central.blogspot.com'>Val A1</a></td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>Val A2</td><td>Val B2</td><td>Val C2</td></tr>
<tr><td>Val A3</td><td>Val B3</td><td><font color='#FF0000'><b><i><u>Val C3</u></i></b></font></td></tr>
</table>
</body>
</html>

Here is an example of HTML that, when put in a .xls file will produce even more interesting results:

<html>
<body>
<h1>Excel HTML Examples</h1>
<h2>Simple With Formulea</h2>
Formulae work as you would expect, simply include them in =
notation, eg =SUM(A1:A84) etc.
<table border=1>
<tr><th>Col A</th><th>Col B</th><th>Col C</th></tr>

<tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
<tr><td>=LEFT(A7,LEN(A7)-1) & (RIGHT(A7,1)+1)</td><td>Val B2</td><td>Val C2</td></tr>

<tr><td>=LEFT(A8,LEN(A8)-1) & (RIGHT(A8,1)+1)</td><td>Val B3</td><td>Val C3</td></tr>
</table>
<br>

<h2>Nested Tables & Differing Border Settings</h2>
It would appear that the outer table's border attribute is
ignored and the inner tables are collapsed together.  But the
outer table's bgcolor attribute for the td elements works. We
can also see here that the colspan attribute of th and td works. 
<table>
    <tr><th bgcolor='#000000' colspan=2><font color='#FFFFFF'>Title Over To Cols</font></th></tr>

    <tr>
        <td bgcolor='#AAAAFF'>
            <table border=1>
            <tr><th>Col A</th><th>Col B</th><th>Col C</th></tr>

            <tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
            <tr><td>=LEFT(A7,LEN(A7)-1) & (RIGHT(A7,1)+1)</td><td>Val B2</td><td>Val C2</td></tr>

            <tr><td>=LEFT(A8,LEN(A7)-1) & (RIGHT(A8,1)+1)</td><td>Val B3</td><td>Val C3</td></tr>
            </table>

        </td>
        <td bgcolor='#FFAAAA'>
            <table border=4>
            <tr><th>Col A</th><th>Col B</th><th>Col C</th></tr>

            <tr><td>Val A1</td><td>Val B1</td><td>Val C1</td></tr>
            <tr><td>=LEFT(A7,LEN(A7)-1) & (RIGHT(A7,1)+1)</td><td>Val B2</td><td>Val C2</td></tr>

            <tr><td>=LEFT(A8,LEN(A7)-1) & (RIGHT(A8,1)+1)</td><td>Val B3</td><td>Val C3</td></tr>
            </table>

        </td>
    </tr>
</table>

</body>
</html>

As always, for more like this check out nerds-central.blogspot.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

alex turner


Member
I started out as a Chemist, but just keep being pulled back into IT.

I am now a senior architect with The Project Network (www.project-network.com)
Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
QuestionHow to creat two sheet?? Pinmemberamber.chang21:10 15 Aug '07  
Generalwhere is the grid? Pinmemberhen1009:34 29 Apr '07  
Generalhow to set page margin of excel and print style [modified] Pinmemberdddd2181:25 24 Feb '07  
GeneralRe: how to set page margin of excel and print style Pinmemberalex turner3:48 25 Feb '07  
Generalalso to mention Pinmemberdvhh3:10 20 Feb '07  
GeneralRe: also to mention Pinmemberalex turner3:20 20 Feb '07  
GeneralFormulae And Further Layout Techniques Pinmemberalex turner1:50 20 Feb '07  
GeneralFormulas & Functions Pinmemberbdlogic9:06 19 Feb '07  
GeneralRe: Formulas & Functions Pinmembern'Anders22:00 19 Feb '07  
GeneralRe: Formulas & Functions Pinmemberalex turner1:54 20 Feb '07  
GeneralForgot something... PinmemberLeftend7:08 19 Feb '07  
GeneralRe: Forgot something... Pinmemberalex turner0:08 20 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Mar 2007
Editor:
Copyright 2007 by alex turner
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project