Serving an Excel Worksheet to a Browser

Comments

Most backends have libraries to create spreadsheets. Here’s a quick and dirty method using HTML tables that works with any language.

The response type for generating Microsoft Excel worksheets on a server is “application/vnd.ms-excel”. After that, write a simple table (without HTML head), and Excel will convert it to a worksheet. MS Office is quite good at converting CSS and HTML 4 table attributes to Excel, but OpenOffice / LibreOffice is more limited – css won’t work.

Formatting XSL data as HTML tables

From what I can see, this is what you can achieve on both using pure HTML

turn borders on
add the border attribute to the table tag. No need for a value, it’s an on/off switch. Or use border=”0” for no border and border=”1” for borders.
include a formula
just write a formula normally, starting with the = sign
center rows or column heads, and make them bold
use TH tags instead of TD tags
merge cells
use the colspan and rowspan attributes of the td tag
change background color
use the bgcolor attribute on either tr, td, or table tags.
change fonts
use the font tag with the face, size, color attributes. Use the i and b tags for italic and bold.

Sending an HTML table as a spreadsheet from the server

Below is an example in PHP which shows all of the above.

You must be careful that if your filename ends with .php most browsers will refuse to open it as an Excel file. An easy workaround would be to save the file in a folder, say /test/index.php, and access it as /test/.

Comments