I have been trying since long for the past 1 week over the Internet to find a help where I can convert a HTML page with complex table structure to be able to download in excel format without any luck.After struggling for a couple of days I was able to find a solution which I would like to share with you all
Lets say we have a MySQL database where we have stored the data. Now after retrieving the data we have displayed it in the web page below
You can see the complex nature of the table. Our aim is to whatever shape and size the table has, we should be able to export it to excel. For this we will use the help of JQuery. You can either download Jquery or add it directly from the url. Here we will use Google APIs to make a reference to JQuery and add it to the head section
1 | <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script> |
Give the table an id ReportTable. This will help us append the entire table & append it to a hidden datatodisplay which will be submitted to the server for processing. We will use the .clone() method to copy the matched elements as well as all of their descendant elements and text nodes & duplicate elements on a page. the .eq() method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set. Note that the supplied index is zero-based, and refers to the position of the element within the jQuery object, not within the DOM tree.
1 2 3 | onsubmit='$("#datatodisplay").val( $(" <div>").append( $("#ReportTable").eq(0).clone() ).html() )'> </div> |
You can download the entire working code here.

















Guess a simple way would be to avoid DOM Manipulation completely.
It can be done on the server side itself, by passing a get parameter to default.php (in form default.php?export=xls). Then, using this parameter (with a php if else statement) to display the just the table (without the HTML head) sending the content-type as done in exporttoexcel.php
So instead of a form submission you only have a link default.php?export=xls for the export button.
can you show how? a working example would help
You can check a working example here: http://testingjay.x90x.net/xls_export_test/
Hey Jay this seems cool will try it out for sure.