Export XLS content in Wordpress admin

Asked

Viewed 281 times

1

I need to export specific content to XLS within the administrative area, and am 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 admin page, it ends up exporting other content than the variable value $html, as menus and admin links within the XLS file.

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

  • I was able to solve it by combining my code with this example here: http://wordpress.stackexchange.com/questions/144156/create-dynamic-wordrpess-blank-page?answertab=oldest#tab-top

  • Hi, Robson, can please post a reply, so help others who do not speak English. I also have a similar script: How to export comments in Wordpress?

  • Hello brasofilo, you say publish a reply with the full code, showing how I managed to solve my problem?

  • 2

    In fact, your question is here without an answer/solution published below. It is nice for those who visit Sopt to see the solution right here. And as you mention that solved the problem, it is good practice to answer the question itself, so in addition to getting help for your questions, you also contribute with the site :)

  • 1

    Got it... okay, I’ll post the full code.

1 answer

2


Follows complete code of how I managed to solve 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 wordpress admin (no 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 marked the first argument of the function add_submenu_page 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 makes the export inside the 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’s useful to someone. D

Browser other questions tagged

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