1

I have a PHP file with a query in sqlsrv, runs well, generated an Excel file with either PHPExcel or without the library, the Excel file is generated either but in the next cell down after writing the results writes strange symbols like this:

ÐÏࡱá;þÿ þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿÿÿ

þÿÿÿþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿþÿà…ŸòùOh«‘+'³Ù0¸@Hh€˜ ¤°äUntitled SpreadsheetUnknown CreatorUnknown Creator@ÐM¦$Ò@ÐM¦$Ò » ÌÑB°=¼%r8X"1ÜCalibriàõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À àõÿ À à À “€ÿ’â8ÿÿÿÿÿÿÿÿÿÿÿÿ€€€€€€€€€ÀÀÀ€€€™™ÿ™3fÿÿÌÌÿÿffÿ€€fÌÌÌÿ€ÿÿÿÿÿÿ€€€€€ÿÌÿÌÿÿÌÿÌÿÿ™™Ìÿÿ™ÌÌ™ÿÿÌ™3fÿ3ÌÌ™ÌÿÌÿ™ÿfff™–––3f3™f333™3™3f33™333…d WorksheetÁÁgæ®üXNombreDepartamentoFecha12/Oct/2016 » ÌÑ*+‚€Áƒ„ffffffæ?'ffffffæ?(è?)è?¡"dXX333333Ó?333333Ó?U}$ }$ }$ }$ }$ }$ }$ }$ }$ ý ý ý ý ý ý >¶@d‹‹dggÿÿÿÿÿ þÿÕÍÕœ.“—+,ù®0¼HPX`hp xŽä WorksheetFeuilles de calculRoot Entryÿÿÿÿÿÿÿÿ ÀFÐM¦$ÒÐM¦$Ò€SummaryInformation(ÿÿÿÿ ÀFèWorkbookÿÿÿÿÿÿÿÿÿÿÿÿ ÀFdDocumentSummaryInformation8ÿÿÿÿÿÿÿÿÿÿÿÿ ÀFìþÿÿÿþÿÿÿþÿÿÿýÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

Does anyone know how to fix it? I tested locally, on a remote server, with different versions of PHP and it goes the same....

Here´s the code:

<?php

require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

$server = "192.168.1.240";
$info = array("Database"=>"Ariel","UID"=>"sa","PWD"=>"" );
$conn = sqlsrv_connect($server, $info);
$param = array('ReturnDatesAsStrings'=> true);
$opt = array("Scrollable" => SQLSRV_CURSOR_KEYSET);

$objPHPExcel = new PHPExcel();

$fec1 = $_GET["fecha1"];
$fec2 = $_GET["fecha2"];
$suc = $_GET["sucursal"];
$nom = 'Nombre';
$per = 'Personal';
$reg = 'Registro';
$hreg = 'Hora Registro';
$con = 'Concepto';
$fecd = 'Fecha';
$obs = 'Observaciones';
$dep = 'Departamento';

$sql = "select personal, (nombre +' '+apellidopaterno+' '+apellidomaterno) as '$nom', departamento as '$dep'
from personal where nombre like '%$nom%' and estatus = 'Alta' and sucursaltrabajo = '$suc'";

$sql2 = "select (p.nombre +' '+p.apellidopaterno+' '+p.apellidomaterno) as '$nom', ad.personal as '$per',ad.registro as '$reg', ad.horaregistro as '$hreg',ad.concepto as '$con',
ad.fechad as '$fecd', ad.observaciones as '$obs'
from asiste a join asisted ad on a.id= ad.id join personal p on p.personal = ad.personal 
where ad.sucursal = '$suc' and a.fechaemision between '$fec1' and '$fec2' order by ad.personal,ad.fechad";

$query = sqlsrv_query($conn, $sql2);

$query2 = sqlsrv_query($conn, $sql);

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Nombre');
$objPHPExcel->getActiveSheet()->setCellValue('B3', $nom);
$objPHPExcel->getActiveSheet()->setCellValue('A4', 'Departamento');
$objPHPExcel->getActiveSheet()->setCellValue('B4', $dep);
$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Fecha');
$objPHPExcel->getActiveSheet()->setCellValue('I4', date("d/M/Y"));

echo "<table border='1'><tr>";
echo "<th>$nom</th>";
echo "<th>$per</th>";
echo "<th>$reg</th>";
echo "<th>$hreg</th>";
echo "<th>$con</th>";
echo "<th>$fecd</th>";
echo "<th>$obs</th>";

while ($row = sqlsrv_fetch_array($query)) {

    $nom = $row['Nombre'];
    $per = $row['Personal'];
    $reg = $row['Registro'];
    $hreg = $row['Hora Registro'];
    $con = $row['Concepto'];
    $fecd = $row['Fecha'];
    $obs = $row['Observaciones'];

    echo "<tr>";
    echo "<th>".$nom."</th>";
    echo "<td>".$per."</td>";
    echo "<td>".$reg."</td>";
    if (!empty($hreg)) {
        echo "<td>".$hreg."</td>";
    }
    echo "<td>".$con."</td>";
    if (!empty($fecd)) {
        echo "<td>".$fecd->format("Y-m-d H:i:s")."</td>";
    }
    echo "<td>".$obs."</td>";
}

echo "</table>";

//header("Content-Type: application/vnd.ms-excel");
//header("Expires: 0");
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header('content-disposition: attachment;filename="Reporte '.$_GET['fecha1'].'\'\''.$_GET['fecha2'].'.xls"');

header('Content-Type: application/vnd.ms-excel');
header('content-disposition: attachment;filename="Reporte '.$_GET['fecha1'].'\'\''.$_GET['fecha2'].'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

   sqlsrv_close( $conn);
?>
  • we would need to see your code to be of any help –  Oct 12 '16 at 00:09
  • Set the correct http response headers if this is appearing in a browser; make sure that absolutely nothing besides the file save is being sent to your browser... the latter seems more likely given your comment `"in the next cell down"` – Mark Baker Oct 12 '16 at 00:09
  • GAAAHHHHHH!!!! Now you've added the cde, it's easy to understand.... a single request can generate only a single response, either your HTML markup, or an Excel file, but not both..... that's how http works – Mark Baker Oct 12 '16 at 00:11
  • @MarkBaker Sorry, one thinks that by putting the question begs the answer – Ricardo Romero Oct 12 '16 at 00:12
  • @nogad Sorry, there is it... – Ricardo Romero Oct 12 '16 at 00:16
  • So? **@MarkBaker** do you have any answer? I´ve copied the code from another question you answered about PHPExcel... – Ricardo Romero Oct 12 '16 at 21:32

1 Answers1

4

Add the following code before header

ob_end_clean();
affaz
  • 1,191
  • 9
  • 23