How to Export excel data in php with jquery

default.php



Export to excel in php




onsubmit='$("#datatodisplay").val( $("
").append( $("#ReportTable").eq(0).clone() ).html() )'>


























First Name Last Name Country

Adeel

Fakhar

Pakistan

Zeeshan

Butter

England

Neil

Johnson

United Kingdom

Diala

Katherine

America








 







exporttoexcel.php

header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=export.xls');
// Fix for crappy IE bug in download.
header("Pragma: ");
header("Cache-Control: ");
?>


In the onSubmit event of form, I am appending the div with ReportTable table. Using clone() method of jquery, I am making the clone of the ReportTable table that I wanted to export to excel, and in the end I am assigning the html of that cloned table to hidden field available in my form so that when form is posted then the data will be transferred to the exporttoexcel.php web page and in that web page I have all my logic for exporting data to excel in php.

Copy/paste this example, run the page and enjoy.

Note: - For this example to run properly, there must be no declaration of inline css in your table or div that you want to export. Whenever you use the style attribute to give inline css, the export to excel functionality will not be performed according to your expectations.

Later on, in this blog I will tell you other methods for this purpose; those methods will perform exporting with all the formatting that you have applied to the data of your table.

If you want to share your methods/techniques for export to excel in php then don’t hesitate to share with the rest of world through our blog.

0 comments:

Post a Comment