Hi, I have to create a excel file with some records using php and xml, the error that I'm getting right now is that the script downloads itself, it seems that instead of excel file I'm creating a php file, can you help me with this issue?
Here is my code:
<?php
/*
Despliega toda la nomina de empleados por departamento
formato: excel via xml
*/
session_start();
if (@$_SESSION['auth'] !="yes")
{
header("Location: nologin.php");
exit();
}
else{
include("vars.php");
$conn=mysqli_connect($host, $user, $password) or die("No se pudo establecer comunicacion");
$db=mysqli_select_db($conn, $basedatos) or die("Error en la base de datos");
$sql="SELECT id, nombres, apellidos, nivacad, depto FROM empleados ORDER BY depto";
$ersdepall=mysqli_query($conn, $sql) or die(mysqli_error($conn));
$efilas=mysqli_num_fields($ersdepall); //podria servir para la paginacion
//xml schema doc:
$datoswriter = " <?xml version='1.0' ?>
<?mso-application progid='Excel.Sheet' ?>
<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office-excel' xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40'>
<Worksheet ss:Name='Emp x Depto'>
<Table>
<Row>";
//encabezados
for($i=1; $i < $efilas; $i++){
$datoswriter .= "<Cell><Data ss:Type='String'>" .mysqli_fetch_fields($ersdepall, $i) . "</Data></Cell>";
}
$datoswriter .= "</Row>";
//display data
while($campos = mysqli_fetch_row($ersdepall)){
$datoswriter .= "<Row>";
for($j=1; j<= $efilas; $j++){
$datoswriter .= "<Cell><data ss:Type='String'>" . $campos[$j] . "</Data></Cell>";
}
$datoswriter .= "</Row>";
}
$datoswriter .= "</Table></Worksheet></Workbook>";
header("Content-type: aplication/octet-stream");
header("Content-Disposition: attachment; filename=reportes.xls;");
header("Content-Type: application/ms-excel");
header("Pragma: no-cache");
header("Expires: 0");
//print all xml+data
echo $datoswriter;
mysqli_close($conn);
}
?>
Regards