I am trying to export data from my database to Excel using PHPExcel. I used the code from this question. Edited mysql to mysqli and changed the extension to xlsx format. But when I want to open the file I get the following error (I translated it into English so sorry for my bad English if it isn't right)
The file rapportage 29-05-2017.xlsx can't be opened because the file format or file extension is not valid. Check if the file isn't damaged and if the file extension matches with the format of the file.
This is the code I copied and changed:
<?php
require 'database.php';
require '../Classes/PHPExcel.php';
if (isset($_POST['exporteer'])) {
$a = $_POST['organisatie_naam'];
$b = $_POST['begin_datum'];
$c = $_POST['eind_datum'];
$datum = date("d-m-Y");
$objPHPExcel = new PHPExcel();
$query = "SELECT * FROM register WHERE organisatie_naam = '$a' AND (registratie_aansluit_datum BETWEEN '$b' AND '$c'";
$result = mysqli_query($db, $query);
$objPHPExcel = new PHPExcel();
$rowCount = 1;
while ($row = mysqli_fetch_array($result)) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$rowCount, $row['registratie_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$rowCount, $row['registratie_soort']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$rowCount, $row['organisatie_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$rowCount, $row['apparaat_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$rowCount, $row['client_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$rowCount, $row['dienst_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$rowCount, $row['functie_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('H'.$rowCount, $row['kosten_soort']);
$objPHPExcel->getActiveSheet()->setCellValue('I'.$rowCount, $row['status_naam']);
$objPHPExcel->getActiveSheet()->setCellValue('J'.$rowCount, $row['registratie_aansluit_datum']);
$objPHPExcel->getActiveSheet()->setCellValue('K'.$rowCount, $row['registratie_afsluit_datum']);
$objPHPExcel->getActiveSheet()->setCellValue('L'.$rowCount, $row['registratie_omschr']);
$rowCount++;
pr($objPHPExcel);
}
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="rapportage ' . $datum . '.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
header("Location: ../rapportage.php");
}
?>
Anyone who knows if I maybe missed something? Thanks in advance!