Export Table Value To Excel Using PHP

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">
    <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.

http://nice-tutorials.blogspot.com/2011/06/export-to-excel-in-php-with-jquery.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: