Export to excel in php with jquery

In this programming tutorial we will learn how to perform export to excel in php. I have already written a post on same topic in asp.net as well. In php there are various methods for this purpose. In this tutorial I will discuss the very basic method that will export the plain data in table to excel. Let’s have a look over the example given below.
Export to excel in php with jquery
default.php
<html>
<head>
<title>Export to excel in php</title>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<style type="text/css">
.myClass
{
font-family:verdana;
font-size:11px;
}
</style>
</head>
<body>
<form action="exporttoexcel.php" method="post" 
onsubmit='$("#datatodisplay").val( $("<div>").append( $("#ReportTable").eq(0).clone() ).html() )'>
  <table id="ReportTable" width="600" cellpadding="2" cellspacing="2" class="myClass">
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Country</th>
    </tr>
    <tr>
      <td><center>
          Adeel
        </center></td>
      <td><center>
          Fakhar
        </center></td>
      <td><center>
          Pakistan
        </center></td>
    </tr>
    <tr>
      <td><center>
          Zeeshan
        </center></td>
      <td><center>
          Butter
        </center></td>
      <td><center>
          England
        </center></td>
    </tr>
    <tr>
      <td><center>
          Neil
        </center></td>
      <td><center>
          Johnson
        </center></td>
      <td><center>
          United Kingdom
        </center></td>
    </tr>
    <tr>
      <td><center>
          Diala
        </center></td>
      <td><center>
          Katherine
        </center></td>
      <td><center>
          America
        </center></td>
    </tr>
  </table>
  <table width="600px" cellpadding="2" cellspacing="2" border="0">
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="center"><input type="hidden" id="datatodisplay" name="datatodisplay">
        <input type="submit" value="Export to Excel">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
exporttoexcel.php
<?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: ");
echo $_REQUEST['datatodisplay'];
?>
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.

So that’s it.
I love your feedback.

Update Note at 05-Oct-2011

Hay guys i really apologize that i miss an important line in the end of exporttoexcel.php page, the line is
echo $_REQUEST['datatodisplay'];
The above mentioned line of code will export the table data to excel. The absence of this line causing the generation of blank excel file that was also complained below by users in comments section . Now tutorial has been updated, I'm sorry for inconvenience.

0 comments: