Export HTML table to Excel

Asked

Viewed 573 times

2

I have this PHP page that lists all my records, I’m trying to export it to Excel, but all the records come out in just one column.

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/classes/xajax.inc.php';

$url = "http://".$_SERVER["SERVER_NAME"]."/relatorios/SaldoDevedorAssociados/itens.ajax.php?" . http_build_query($_GET);

$json = file_get_contents($url);    

$_json = json_decode($json);

$somaCc = 0;
$somaComprometido = 0;
$somaTotal = 0;

if (is_object($_json)) {
    foreach ($_json->aaData as $item) {
        $matricula = $item[0];
        $nome = $item[1];
        $saldoCc = $item[2];
        $saldoComprometido = $item[3];
        $saldoTotal = $item[4];

        $tr .= "
            <tr class='linha'>
                <td>$matricula</td>
                <td>$nome</td>
                <td class='right'>$saldoCc</td>
                <td class='right'>$saldoComprometido</td>
                <td class='right'>$saldoTotal</td>
            </tr>
        ";

        $somaCc += Conversao::formataFloatToSql($saldoCc);
        $somaComprometido += Conversao::formataFloatToSql($saldoComprometido);
        $somaTotal += Conversao::formataFloatToSql($saldoTotal);
    }

    $tabelaItens = "<table><tr class='cabecalho'>";

    $tr .= "<tr class='linha'>
    <td></td>
    <td></td>
    <td class='right'><strong>$somaCc</strong></td>
    <td class='right'><strong>$somaComprometido</strong></td>
    <td class='right'><strong>$somaTotal</strong></td>
    </tr>";

    $tabelaItens .=     
        "<th>Matrícula</th>
        <th>Nome</th>
        <th class='right'>Saldo Conta Corrente</th>
        <th class='right'>Saldo Comprometido</th>
        <th class='right'>Saldo Total</th>
        </tr>
        $tr
        </table>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <?php $xajax->printJavascript("http://" . $_SERVER['HTTP_HOST'] . "/scripts/xajax/"); ?>

        <title>Sistema FUSERGS</title>

        <?php require_once $_SERVER['DOCUMENT_ROOT'] . "/scripts/inc/jqueryAlertas.inc.php" ?>

        <?php require_once $_SERVER['DOCUMENT_ROOT'] . "/scripts/inc/menu.inc.php" ?>

        <?php require_once $_SERVER['DOCUMENT_ROOT'] . "/scripts/inc/jquery.tools.inc.php" ?>

        <?php require_once $_SERVER['DOCUMENT_ROOT'] . "/scripts/inc/hash.inc.php" ?>

        <?php require_once $_SERVER['DOCUMENT_ROOT']."/scripts/inc/jqueryFormatCurrency.inc.php" ?>
        <link rel="stylesheet" href="/css/folha.css" type="text/css" />

        <?php echo $cssDinamico; ?>
    </head>

    <body style="background-color: white; font-family: Arial, Tahoma, Verdana; font-size: 15px;">
    <input type="button" id="btnExport" value="Exportar para Excel" />
        <div id="conteudoVisualizacao">
            <div>
                <img style="height: 80px; margin-right: 10px;" id="logomarca" src="http://<?php echo $_SERVER['HTTP_HOST'] ?>/images/Logo.jpg" />
                <span id="titulo" style="
                      font-family: Arial,Tahoma,Verdana;
                      font-size: 22px;
                      font-weight: bold;
                      position: absolute;
                      text-align: center;
                      top: 26px;
                      width: 639px;
                      ">Fundação dos Servidores do <br />SESI no Rio Grande do Sul</span>
            </div>
            <div style="clear: both">
                <h2 style="text-align: center">Relatório Saldo Devedor Associados</h2>
                <p>Tipo Pessoa: <?php echo $_GET['tipoPessoaVisual'] ?></p>
                <hr/>
                <div id="dadosItensFolha">
                    <?php echo $tabelaItens ?>
                    <b>Total de registros: <?php echo  $_json->iTotalDisplayRecords; ?></b>
                </div>


                <script>
                    $("#btnExport").click(function (e) {
                        window.open('data:application/vnd.ms-excel,' + $('#dadosItensFolha').html());
                        e.preventDefault();
                    });
                </script>

    </body>
</html>

1 answer

-1

Excel: Tab (File)> Supplement > Supplements COM> Inquire Afterward Tab (Data) > Get Data > From Other Sources > From the Web Afterward enter the URL of where the table is Then, if the page has more than one table, a list of tables will be shown so that you choose which table you import. Done this just wait and the table will be available in a new tab in Excel.

Source: Importing data from WEB to Excel, by PROF DARLAN

Browser other questions tagged

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