How to Create an Excel File from a Simple HTML Page

smglo2006
Have you ever needed a way to quickly create an Excel file from information stored in a web page table? If so, read on for a quick and easy solution to the problem. I was recently shown this trick by a fellow coworker when I had a web report that people wanted to receive in an email as an Excel attachment.

I spent time on the Internet researching how to build a new Excel spreadsheet from Java and I found several different API's for doing it. Some of them were free; some of them were not free. I spent several hours learning one of the free API's and experimenting on creating simple Excel spreadsheets with basic cell formatting (borders, background colors, differing fonts, etc).

It was only after all this that I was talking to a co-worker who had a better suggestion for me. Since I didn't need to do anything fancy in the spreadsheet, I really only needed to format daily production numbers into a easy to view format, he suggested that I build an html page and import it into Excel. He explained that if I created an html page with a simple HTML table, change the name to end with .xls (the default Excel file extension), and then open the document in Excel, it would display correctly.

So I tried it and it worked. Since I had to build the HTML table anyways for the web page, I just had to create the code to save the page as the Excel document and attach it to an email. All of the formatting was saved and the page displayed perfectly. No complicated api's were needed and hardly any extra code was needed. Try it yourself. Once you save the file (use testing.xls), double-click and watch it open in excel with the table preserved. For my real world project, I have css formatting built into the top of the document and it transfers right across to the excel document.

There are a couple of caveats to watch out for when using this method to create Excel files. First and foremost, this is only for simple Excel spreadsheets that are for displaying data only. This method will not work for any applications that need to do mathematical functions or anything like that. You need to explore the native api's to do the complicated stuff. Another warning, do not use an external CSS file as it won't be imported and the formatting will be lost. If the formatting is lost, then the document will not look correctly.

I hope this simple tip helps save you some time on your simple Excel spreadsheets.

Published by smglo2006

Father of 3 strapping boys with lots of advice of what not to do.  View profile

1 Comments

Post a Comment
  • enna6/22/2009

    can you give an example because i can't seem to understand it..thank you

To comment, please sign in to your Yahoo! account, or sign up for a new account.