How to add Header to an Excel Report with PHP

1

I need to generate a report in Excel with Cabecera, that is, add who did it, the department, the time I downloaded the excel from the page. etc ... etc ... I already generate the report, but I have no idea how to add the other.

This is the file that is downloaded

header('Content-Type:text/csv; charset=latin1');
        header('Content-Disposition: attachment; filename="Reporte Marcaje.xls"');



        $conn=new PDO('mssql:host=192.168.20.219; dbname=Intranet', 'adam', 'adam219');

          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

         $confech=$conn->query("SELECT * FROM marcaje");

 ?>




<!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" />


    <title>Document</title>
</head>
<body>


        <table class="table" id="ocultarFecha">
            <tr>
            <td class="nombreNegrita">Fecha Entrada</td>
            <td class="nombreNegrita">Hora Entrada</td>
            <td class="nombreNegrita">Cédula</td>
            <td class="nombreNegrita">Nombre</td>
            <td class="nombreNegrita">Cargo</td>
            <td class="nombreNegrita">Departamento</td>
            <td class="nombreNegrita">Fecha de Revista</td>
            <td class="nombreNegrita">Hora de Revista</td>
            <td class="nombreNegrita">Encargado</td>
            <td class="nombreNegrita">Cargo</td>
            <td class="nombreNegrita">Departamento</td>
            <td class="nombreNegrita">Sucursal</td>
            </tr>

                <?php 
                     while($row=$confech->fetch(PDO::FETCH_ASSOC)) {
                 ?>
            <tr>
                <td><?php echo $row['fechEntrada'];  ?></td>
                <td><?php echo $row['horaEntrada']; ?></td>
                <td><?php echo $row['cedula']; ?></td>
                <td><?php echo $row['nombre']; ?></td>
                <td><?php echo $row['cargo']; ?></td>
                <td><?php echo $row['departamento']; ?></td>
                <td><?php echo $row['fechRevista']; ?></td>
                <td><?php echo $row['horaRevista']; ?></td>
                <td><?php echo $row['usuario_sesion']; ?></td>
                <td><?php echo $row['cargo_sesion']; ?></td>
                <td><?php echo $row['departamento_sesion']; ?></td>
                <td><?php echo $row['sucursal_sesion']; ?></td>
            </tr>
        <?php } ?>

        </table>



</body>
</html>

Even, I tried to do it with the PHPExcel library and I do not get anything.

    
asked by Jdavid Barajas 23.05.2018 в 15:21
source

1 answer

0

I'll give you an example that can guide you using the library PhpSpreadsheet , since the PHPExcel library is deprecated.

require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$phpExcel = new Spreadsheet();

//aca seteas las propiedades
$phpExcel->getProperties()
->setCreator('Superman')
->setTitle('un titulo')
->setSubject('algo mas')
->setDescription('una descripcion');
->setLastModifiedBy("Batman")
->setKeywords("palabras clave")
->setCategory("una categoria");

//Seteas los "titulos" de las columnas
$phpExcel->setActiveSheetIndex(0)
     ->setCellValue('A1','Fecha Entrada')
     ->setCellValue('B1','Hora Entrada')
     //todas las demas columnas
     ->setCellValue('C1','Sucursal Sesion');

//Luego podes meterle los datos de la consulta asi
$fila = 2;
while($row=$confech->fetch(PDO::FETCH_ASSOC)) {
    $phpExcel->setActiveSheetIndex(0)
    ->setCellValueByColumnAndRow(1, $fila , $row["fechEntrada"])
    ->setCellValueByColumnAndRow(2, $fila , $row["horaEntrada"])
    //todas las demas columnas
    ->setCellValueByColumnAndRow(12, $fila , $row["sucursal_sesion"]);

    $fila++;
}

$writer = new Xlsx($phpExcel);
$writer->save('MiExcel.xlsx');
    
answered by 23.05.2018 в 16:17