Excel printing with php and jquery

Asked

Viewed 746 times

2

I am trying to print to excel a query presented in table with php and jquery in tag <tbody>. However it did not work. It presented in excel a wrong result with all the result HTML in a single cell. COULD HELP ME?

I found the solution on the site http://nice-tutorials.blogspot.com.br/2011/06/export-to-excel-in-php-with-jquery.html

HTML and PHP

<form action="frameworks/teste.php" method="post"
    onsubmit='$("#datatodisplay").val( $("<div>").append( $("#minhaTabela").eq(0).clone() ).html() )'>
    <table id="minhaTabela" class="table table-striped">
        <thead>
            <th>DM Clarity</th>
            <th>Mantis</th>
            <th>Sistema</th>
            <th>Descrição</th>
            <th>Situação</th>
            <th>Prioridade</th>
            <th>Previsão Homol.</th>
            <th>Ações</th>
        </thead>
        <tbody>
        </tbody>
    </table>
    <div align="center">
        <input type="hidden" id="datatodisplay" name="datatodisplay">
        <button type="submit" class="btn btn-primary">Export to Excel</button>
    </div>
</form>
<?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'];  
?>

2 answers

1

Your problem is that the output should be after sending the headers.

Function PHP header() (English)

<?php

$nomeFicheiro = "bubu.xls";

$html = "<table><tr><td>Celula A1</td><td>Celula B1</td></tr></table>";

header("Content-type: application/vnd.ms-excel");

header("Content-Disposition: attachment; filename=$nomeFicheiro");

echo $html;

?>

To generate a . XLSX you must send the header:

header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

Note: Currently this technique causes Excel to generate a warning when opening the file.


Solution credits for this reply on the SOEN posted by @Nullpoiиteя.

This answer in the SOEN presents a possible solution to circumvent the warning, but requires a more extensive Markup.

0

I believe that this excel example with jquery and php does not work, in the blog comments themselves already complain.

Use this library made by the Excel programmers themselves.

https://github.com/PHPOffice/PHPExcel

Already used it, is very good and well complete, serves both to export and to import.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.