Hi, I have to generate an excel file from a php scripts, but I'm having an error: the browser tries to download the php file instead of create the excel file. My question is: what line do I have to change to generate the excel file?
Regards
<?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' encoding='utf-8' ?>
<?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-Type: application/ms-excel");
header("Content-Disposition: attachment; filename=reportes.xls;");
header("Pragma: no-cache");
header("Expires: 0");
//print all xml+data
echo $datoswriter;
mysqli_close($conn);
}
?>