0

I'm trying to export data from Mysql and php to an excel file.

but something is wrong.

this is my code:

<?php
header('Content-Type: text/html; charset=UTF-8'); 
session_start();

if(!isset($_SESSION['usuario'])) 
{
  header('Location: login.php');  
}

require('../conexion.php');
  $conexion = conexion();

    if (mysqli_connect_errno()) 
    {
        printf("La conexión con el servidor de base de datos falló: %s\n", mysqli_connect_error());
        exit();
    }

    $consulta =  $_SESSION['query'];
    echo $consulta; //consulta = SELECT * FROM cotion where status= 'Vea' 
    $resultado = mysql_query ($consulta, $conexion) or die (mysql_error ());
 $registros = mysql_num_rows ($resultado);

 if ($registros > 0) {
   require_once 'excel/Classes/PHPExcel.php';
   $objPHPExcel = new PHPExcel();

   //Informacion del excel
   $objPHPExcel->
    getProperties()
        ->setCreator("Creator")
        ->setLastModifiedBy("setLastModifiedBy")
        ->setTitle("tittle")
        ->setSubject("Subject")
        ->setDescription("Description")
        ->setKeywords("Keywords")
        ->setCategory("Category");    

   $i = 1;    
   while ($registro = mysql_fetch_object ($resultado)) {

      $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $registro->pais);

      $i++;

   }
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="ex.xlsx"');
header('Cache-Control: max-age=0');

$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');
exit;
mysql_close ();
?>

But i only can see it:

enter image description here

but I do not see it in the excel file, I see it in the web browser.

I looked here to do my exports, and download the PHPExcel class from here

Any idea about what could Ido?

Thank you

3
  • You are setting a header at the top that says the file type is text/html. Try removing that. Commented Jan 15, 2015 at 19:32
  • I removed the header but, Still seeing the same ��s Commented Jan 15, 2015 at 19:35
  • I just deleted the echo as @Mark Baker said.. But the problem continues Commented Jan 15, 2015 at 19:41

1 Answer 1

1

Note the caution in the PHPExcel developer docs section on Redirecting output to a client’s web browser:

Caution:

  • Make sure not to include any echo statements or output any other contents than the Excel file. There should be no whitespace before the opening <?php tag and at most one line break after the closing ?> tag (which can also be omitted to avoid problems).
  • Make sure that your script is saved without a BOM (Byte-order mark). (Because this counts as echoing output)
  • Same things apply to all included files

Note also that if you're using some kind of framework, it may be sending different headers, or generating additional outputs. Most frameworks provide some mechanism to allow you to override that behaviour.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.