Translation


by Transposh

Export complex HTML Tables to Excel

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

HTML generated after fetching data from database

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

?View Code JAVASCRIPT
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.

?View Code JAVASCRIPT
1
2
3
onsubmit='$("#datatodisplay").val( $("
<div>").append( $("#ReportTable").eq(0).clone() ).html() )'>
</div>

You can download the entire working code here.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Technorati
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • blogmarks
  • Bloglines
  • BlogMemes
  • Ask
  • DZone
  • Blogosphere News
  • De.lirio.us
  • VoteForIt

6 Responses to “Export complex HTML Tables to Excel”

Comments

  1. Jay says:

    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.

  2. Girish says:

    Hey Jay this seems cool will try it out for sure.

Pingbacks & Trackbacks

  1. Informasi Online Perkumpulan Warga Jawa Kelahiran di Sumatera…

    [...]Export complex HTML Tables to Excel | Ajatus Software – A software developing and consulting company[...]…

  2. treadmill workouts to lose weight…

    [...]Export complex HTML Tables to Excel | Ajatus Software – A software developing and consulting company[...]…

Leave a Reply