How to export to Excel with php and Sql Server 2008


I want to generate an excel report of my markup query. but I could not.

I get the following error in the excel.

<b>Fatal error</b>:  Class 'Conexion' not found in <b>C:\xampp\htdocs\intragarzon2.0\formatosTH\marcaje\core\bin\ajax\goFech.php</b> on line <b>16</b><br />

this is my connection


    class Conectar{

      public static function conexion($host){


          $conect=new PDO('mssql:host='.$host.'; dbname=Empleados', 'sa', '');
          // $conect=new PDO('mssql:host=; dbname=Empleados', 'sa', '');
          $conect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          // $conect->exec("SET CHARACTER UTF8");

        }catch(Exception $e){

          die ("Error " . $e->getMessage());
          echo "Línea del Error " . $e->getLine();


        return $conect;



and this is code to export the query


            header('Pragma: public'); 
            header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past    
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
            header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1 
            header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 
            header('Pragma: no-cache'); 
            header('Expires: 0'); 
            header('Content-Transfer-Encoding: none'); 
            header('Content-Type: application/'); // This should work for IE & Opera 
            header('Content-type: application/x-msexcel'); // This should work for the rest 
            header('Content-Disposition: attachment; filename="Reporte Marcaje.xls"');

            $adam = new Conexion();

            $desde = $_POST['desde'];
            $hasta = $_POST['hasta'];

            if(isset($desde) == false) {
                $desde = $hasta;

            if(isset($hasta)==false) {
                $hasta = $desde;

            //  $j=0;

            $confech=$re->query("SELECT * FROM marcaje WHERE fechRevista BETWEEN '$desde' AND '$hasta'");


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<div class="contariner">
     <div class="row">
        <table width="60%" border="0"  align="center" cellpadding="0" cellspacing="0" class="table table-condensed" id="ocultarFecha">
            <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="col-sm-5 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>
            <td><a href="formatosTH/marcaje/core/bin/ajax/goFech.php" class="btn btn-success">Exportar</a></td>

                    while($row=$confech->fetch(PDO::FETCH_ASSOC)) {

                <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>
        <?php } ?>



Also, on the page I do not get the results of the query? why will it be?

asked by Jdavid Barajas 17.05.2018 в 14:10

1 answer


The problem is that you are instantiating a class Conexion() , but your class is Conectar , it should be:


      $adam = new Conectar();

answered by 17.05.2018 / 14:16