Exporting data from php to xls from a link

2

I am making a link, which when the user clicks on it should generate an xls file. The link has the date-attributes and should pass these attributes to the page.php and generate the xls. I tried jquery and it is not working, does it have any other way?

<a href="#" id="xls" data-unidade="203" data-tema="1">
    Gerar XLS
</a>

The jquery

$(document).ready(function() {
    $('#xls').click(function(event) {
        event.preventDefault();

        $.ajax({
            type: 'POST',
            url : 'produto/exportXls',
            data: {
                'tema_id' : $(this).data('tema'),
                'unidade_id' : $(this).data('unidade')
            }
        });
    });
});

The php that creates the table to generate the xls

$html = '<table><tr>';
$html .= '<td colspan="3">Planilha teste</tr>';
$html .= '</tr>';
$html .= '<tr><td><b>Coluna 1</b></td><td><b>Coluna 2</b></td><td><b>Coluna 3</b></td></tr>';
$html .= '<tr><td>L1C1</td><td>L1C2</td><td>L1C3</td></tr>';
$html .= '<tr><td>L2C1</td><td>L2C2</td><td>L2C3</td></tr>';
$html .= '<tr><td>L3C1</td><td>L3C2</td><td>L3C3</td></tr>';
$html .= '</table>';

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/x-msexcel");
header("Content-Disposition: attachment; filename=arquivo.xls" );
header("Content-Description: PHP Generated Data" );
// Envia o conteúdo do arquivo
echo $html;

But it's not rolling ...

    
asked by anonymous 22.07.2014 / 19:12

2 answers

3

In a free translation of the words of John Culviner, creator of the plugin jQuery File Download :

  

JavaScript, by design, does not have the ability to perform low-level tasks on your users' computer for security reasons. Initializing a download dialog is one such limitation.

So, anyway, you'll have to generate the file on your server and have a valid URI that points to it. With PHP you can do something simple like for example using file_put_contents ()

Once this is done, a rough solution would succeed in callback to manually redirect the browser to the URI of that resource:

$.ajax({
    url: 'path/to/server/action',
    data: {
        'tema_id': $(this).data('tema'),
        'unidade_id': $(this).data('unidade')
    },
    success: function (data) {
        window.location.href='arquivo.xls';
    }
});

But this is not very appealing to modern web standards. With the above plugin, not only do you make up a specific solution as it offers you a more elegant way of handling errors.

When the server responds by prompting the response to be treated as a download, if it succeeds, the browser does not change the page the user is on and displays the appropriate dialog box.

But when there is an error (file does not exist, for example), the browser is redirected to the default page of 404 which may even be written in Korean which is frustrating to the user.

    
22.07.2014 / 20:47
1

1) Change the call to PHP to a traditional link and not AJAX;

2) add the following header to force the download:

header("Content-type: application/force-download");  
    
22.07.2014 / 19:44