Reputation: 85
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:
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
Upvotes: 0
Views: 216
Reputation: 212412
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.
Upvotes: 1