0

Converter table em .XLS

1º passo o código PHP


public function exportarAction() {
        $this->layout = false;
        $db = new $this->model();
        $this->views['dados'] = $db->Lista();
    }

2º passo o codigo HTML

<?php

header("Pragma: public");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: pre-check=0, post-check=0, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Transfer-Encoding: none");
header("Content-Type: application/vnd.ms-excel;");
header("Content-type: application/x-msexcel");
header("Content-Disposition: attachment; filename=produtos-" . date('Ymd') . ".xls");

?>



<html>

    <body>

        <table border="1">
            <tr>
                <th style="background: #D7D7D7">CODIGO</th> 
                <th style="background: #D7D7D7">NOME</th>
                <th style="background: #D7D7D7">MARCA</th>
                <th style="background: #D7D7D7">VALOR</th>

            </tr>

            <?php foreach($THIS->views['dados'] as $row): ?>
                <tr>
                    <td align="center">
                        <?= utf8_decode($row["codigo"]); ?>
                    </td>
                    <td align="center">
                        <?= utf8_decode($row["nome"]); ?>
                    </td>
                    <td align="center">
                        <?= utf8_decode($row["nome_marca"]); ?>
                    </td>
                    <td align="center">
                       R$ <?php echo number_format($row['valor'], 2, ',', '.');?>
                    </td>
                </tr>

            <?php endforeach; ?>

        </table>

    </body>

</html>

Deixe uma resposta