Printing in excel with php and jquery

2

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

I found the solution on the link

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'];  
?>
    
asked by anonymous 03.07.2014 / 19:53

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 should send the header:

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

Note: This technique currently causes Excel to generate a warning when opening the file.

Solution Credits for this answer in SOEN by @ NullPoiиte.

This answer in SOEN presents a possible solution to circumvent the warning, but it does require a longer markup.

    
04.07.2014 / 01:12
0

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

Use this library made by the excel programmers themselves.

link

I have used it, it is very good and very complete, it serves both to export and to import.

    
04.07.2014 / 01:37