Export XLS content in WordPress admin

1

I need to export specific content to XLS within the administrative area, and I'm using the following code:

$arquivo = 'planilha.xls';

$html = '';
$html .= '<table>';
$html .= '<tr>';
$html .= '<td colspan="3">Planilha teste</tr>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td><b>Coluna 1</b></td>';
$html .= '<td><b>Coluna 2</b></td>';
$html .= '<td><b>Coluna 3</b></td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td>L1C1</td>';
$html .= '<td>L1C2</td>';
$html .= '<td>L1C3</td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<td>L2C1</td>';
$html .= '<td>L2C2</td>';
$html .= '<td>L2C3</td>';
$html .= '</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}\"" );
header ("Content-Description: PHP Generated Data" );

echo $html;
exit;

But when I run this code on a page in admin, it ends up exporting other content beyond the value of the $html variable, such as admin menus and links within the XLS file.

How to insert only the content returned by the $html variable into the generated XLS file?

    
asked by anonymous 15.07.2015 / 00:39

1 answer

1

Here is a complete code on how I solved the problem:

I created a file called "export.php"

<?php

ob_start();

global $wpdb;

// Tabela do banco de dados
$minha_tabela = $wpdb->prefix . "tabela_de_registros";

$exportar_resultados = $wpdb->get_results("SELECT * FROM $minha_tabela ORDER BY id DESC");

// Nome do arquivo que será exportado
$arquivo = 'Relatorio_'.date('dmYHis').'.xls';

// Tabela HTML com o formato da planilha
$html = '';
$html .= '<table border="1">';
$html .= '<tr>';
$html .= '<td colspan="4" align="center"><b>Relatorio do site '.get_bloginfo('name').'</b></tr>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td><b>ID</b></td>';
$html .= '<td><b>Valor 1</b></td>';
$html .= '<td><b>Valor 2</b></td>';
$html .= '<td><b>Valor 3</b></td>';
$html .= '</tr>';

foreach($exportar_resultados as $resultado){

    $html .= '<tr>';
    $html .= '<td>'.$resultado->id.'</td>';
    $html .= '<td>'.$resultado->valor_1.'</td>';
    $html .= '<td>'.$resultado->valor_2.'</td>';
    $html .= '<td>'.$resultado->valor_3.'</td>';
    $html .= '</tr>';

}

$html .= '</table>';


// Configurações header para forçar o download
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/vnd.ms-excel; charset=utf-8");
header ("Content-Disposition: attachment; filename=\"{$arquivo}\"" );
header ("Content-Description: PHP Generated Data" );

// Envia o conteúdo do arquivo
echo $html;

ob_end_flush();
//exit;

?>

function that creates a blank page in the admin of wordpress (without admin menus, links, etc ...)

function my_menu_pages() {
    $hook = add_submenu_page(null, 'Exportar Relatório', 'Exportar Relatório', 'administrator', 'exportar-relatorio', function() {
    }
    );
    add_action('load-' . $hook, function() {

        require_once dirname(__FILE__)."/export.php"; //chamamos o arquivo export.php
        exit;
    });
}

I checked the first argument of the add_submenu_page function as null , to prevent the export page from being accessed through the wordpress admin menu, so it will only be accessed directly by the url.

Finally, the button that exports into wordpress admin:

<a href="<?php echo admin_url( 'admin.php?page=exportar-relatorio' ); ?>" class="button button-primary action">Exportar para XLS</a>

I hope it will be useful to someone. ; D

    
17.07.2015 / 16:49