Export html to ms excel file in php

In this article,I will explain how to export html to excel(.xls) file. we are going to export an HTML table (or any html data) to a MS Excel document as it is displayed on the HTML page.
it is very easy to export HTML data to excel in PHP. Lets see Below code


if(isset($_POST[‘excel’]) && $_POST[‘excel’])


# Download Excel (.xls) File…

header(‘Content-Type: application/force-download’);

header(‘Content-disposition: attachment; filename=ExportHtmlToExcel.xls’);

header(“Pragma: “);

header(“Cache-Control: “);

echo $_POST[‘excel’];







function getHtmlData()


$(“#excel”).val(‘<table border=”1″>’+$(“#info”).clone().html()+'</table>’);

return true;




<title>Export HTML to Excel in PHP</title>

// <![CDATA[
// ]]>



<form method=”post” onSubmit=”javascript:return getHtmlData()”>

<table border=”1″ id=”info”>


<th>First Name : </th>

Last Name :



Test First Name

<td>Test Last Name</td>



<input type=”hidden” id=”excel” name=”excel” value=””>


<input type=”submit” value=”Export HTML to Excel”>





  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: