Behseini
Behseini

Reputation: 6330

HTML Table to Ms Excel Using PHP and jQuery

I am trying to export an HTML existing table to MS Excel with assigned Name. As you know exporting HTML Table to Excel file can be achieved by jQuery BUT as I said I have to add a Name to File as well.

HTML

 <button id="myButtonControlID">Export Table to Excel</button>
  <div id="divTableDataHolder">
    <title>Demo for huge data</title>
    <table>
        <tbody>
            <tr>
                <td>abc</td>
                <td>12</td>
             </tr>
            <tr>
                <td>abc</td>
                <td>12</td>
            </tr>
        </tbody>
    </table>
</div>

So Far I have a jquery which pass data from html to PHP file as:

$( document ).ready(function() {
    var naem = "MyExcel";
    $("[id$=myButtonControlID]").click(function (e) {
    window.open('getMyXSL.php?data=' + encodeURIComponent($('div[id$=divTableDataHolder]').html())+'&name='+name);
    e.preventDefault();
});
});

and in getMyXSL.php file I have

<?php
    header('Content-Disposition: attachment; filename="'.$_REQUEST['name'].'"');
    header('Content-Type: application/vnd.ms-excel');
    echo($_REQUEST['data']);
?>

but when I the buton clicked file generates/download a PHP file called getMyXSL.php which looks like this

<title>Demo for huge data</title>
<table>
    <tbody>
        <tr>
            <td>abc</td>
            <td>12</td>
        </tr>
        <tr>
            <td>abc</td>
            <td>12</td>
        </tr>
    </tbody>
</table>

Can you please let me know how to fix this and how I can export the table to .xls with a name? Thanks

enter image description here

Upvotes: 1

Views: 3709

Answers (3)

Joseph Stalin
Joseph Stalin

Reputation: 140

var naem = "MyExcel"; 

It should be name, that is the error.

Upvotes: 0

Praveen Srinivasan
Praveen Srinivasan

Reputation: 1620

    <table border="1" id="ReportTable" class="myClass">
        <tr bgcolor="#CCC">
          <td width="100">Fecha</td>
          <td width="700">Seguimiento</td>
          <td width="170">Producto</td>
          <td width="30">&nbsp;</td>
        </tr>
        <tr bgcolor="#FFFFFF">
          <td><?php                 
                $date = date_create($row_Recordset3['fecha']);
                echo date_format($date, 'd-m-Y');
                ?></td>
          <td><?php echo $row_Recordset3['descripcion']; ?></td>
          <td><?php echo $row_Recordset3['producto']; ?></td>
          <td><img src="borrar.png" width="14" height="14" class="clickable" nClick="eliminarSeguimiento(<?php echo $row_Recordset3['idSeguimiento']; ?>)" title="borrar"></td>
        </tr>
      </table>

      <input type="hidden" id="datatodisplay" name="datatodisplay">  
        <input type="submit" value="Export to Excel"> 

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.

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.

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'];
    ?>

Upvotes: 0

Dmitriy.Net
Dmitriy.Net

Reputation: 1500

Extension of output file must be xls, but your script is not generates this correctly. For fix it, you need to change header information:

<?php
$filename = sprintf('%s.xls', rawurlencode(preg_replace('~&#(\d{3,8});~e', '$fixchar(\'$1\')', $_REQUEST['name'])));

header('Content-Disposition: attachment; filename="'.$filename.'";' );
header('Content-Type: application/vnd.ms-excel');
echo($_REQUEST['data']);
?>

Upvotes: 1

Related Questions