I am making a feature that allows the user to telecahrge table data directly into excel. But I have a problem when saving the file. I write my file with php spreadsheet, then I try to save it, before downloading the created file.
But I can’t save it. Do you know why?
My button to dl:
<td style='border:1px solid grey; border-top:0; border-bottom:0; border-left:0; border-right:0;' align='center' colspan='1'>
<?php
$linkXlsx = './inc/fin/test/'.$_POST['soct'].date('Y-m-d').'.xlsx';
?>
<a href='<?php echo $linkXlsx; ?>' download>
<img src='./img/excel.png' height='30' alt='Télécharger' />
</a>
</td>
My function:
function download () {
$Ecritures = get_ListeEcrituresCegid ( $_POST['soct'] , $_POST['datd'] , $_POST['datf'] );
$NomFichier = $_POST['soct'].date('Y-m-d').'.xlsx';
$Classeur = new Spreadsheet();
$Classeur->getDefaultStyle()->getFont()->setName('Arial Nova');
$Classeur->getDefaultStyle()->getFont()->setSize(9);
$Feuille = $Classeur->getActiveSheet();
$Feuille->setCellValue('A1', 'EXERCICE' );
$Feuille->setCellValue('B1', 'JOURNAL' );
$Feuille->setCellValue('C1', 'PIECE' );
$Feuille->setCellValue('D1', 'COMPTE CLIENT' );
$Writer = new Xlsx($Classeur);
$Writer->save('./inc/fin/test/'.$NomFichier);
$cellColors = [
'A1' => 'd4e6f1', 'B1' => 'd4e6f1', 'C1' => 'd4e6f1',
'D1' => 'd4e6f1',
];
foreach ($cellColors as $cell => $color) {
$Feuille->getStyle($cell)->getFill()->setFillType(Fill::FILL_SOLID);
$Feuille->getStyle($cell)->getFill()->getStartColor()->setRGB($color);
}
$rowIndex = 2;
foreach ( $Ecritures as $e ) {
echo $Ecritures;
$Feuille->setCellValue('A'.$rowIndex, $e['E_EXERCICE']);
$Feuille->setCellValue('B'.$rowIndex, $e['E_JOURNAL']);
$Feuille->setCellValue('C'.$rowIndex, $e['E_DEBIT']);
$Feuille->setCellValue('D'.$rowIndex, $e['E_COMPTECLI']);
$rowIndex++;
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $NomFichier . '"');
header('Cache-Control: max-age=0');
$Writer = new Xlsx($Classeur);
$Writer->save('./inc/fin/test/'.$NomFichier);
return $Writer;
}