Export php generated HTML table to Excel.

Hi
I have a table that has been generated through a long and complicated series of while loops and SQL queries. A user searches for a product and all the parts to that product are generated in a table.

How can I export this to excel? Is there anyway to export this to excel with the cell colors? Please help, thank you

I’m sure there are some libraries or php classes that you can use to convert HTML to Excel spreadsheet. But there is a quick solution that worked for me. Before outputting your HTML to browser, just add these lines at top:
[php]<?php
header(“Content-type: application/vnd.ms-excel”);
header(“Content-Disposition: attachment; filename=your_file_name.xls”);
?>[/php]

This is not really converting html to xls, but this will tell your browser to open the file with MS Excel (and Excel opens HTML tables just as spreadsheet). You can style your table cells with CSS, justify cell content etc. - Excel will recognize all the HTML/CSS formatting.

Oh okay, that makes sense (: Is there anyway I can make it a link? To do that would I have to link to a duplicate of the original page but with this header function?

If you need original file to be on site as HTML page, then yes - you can do duplicate and place these headers. Then link to duplicate file. When clicked in browser, it will prompt to either open in Excel or save as.

Works! Thank you (: One more question though, is there anyway to also import the styling? Because Excel shows an error saying that the style sheet doesn’t exist because it’s not in the download directory o:

For Excel I’d add all styles right into the HTML within rather then using external css file.

Sponsor our Newsletter | Privacy Policy | Terms of Service