0

I am trying to download "Xlsx" file using PhpSpreadSheet and I am sure code is working fine but all I get is these strange characters shows up in my consoleenter image description here

I tried nearly all the solutions here in stackoverflow but all give me the same result below in the image

solutions I have tried: solution more solutions

my code

<?php


require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); 


$sheet->setCellValue('A1', 'ID'); 
$sheet->setCellValue('B1', 'Name'); 
$sheet->setCellValue('C1', 'Name2'); 
$sheet->setCellValue('D1', 'Name3');
 $sheet->setCellValue('E1', 'Type'); 

// Write an .xlsx file  
$date = date('d-m-y-'.substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$filename = "export_".$date.".xlsx";

try {
$writer = new Xlsx($spreadsheet);
    $writer->save($filename);
    $content = file_get_contents($filename);
} catch(Exception $e) {
    exit($e->getMessage());
}

header("Content-Disposition: attachment; filename=".$filename);

unlink($filename);
Hassan
  • 53
  • 1
  • 8
  • `echo $contents;` otherwise you are not actually sending anything but headers – RiggsFolly Sep 07 '21 at 16:52
  • I am filling excel from my Database but I just put the headers here to make it easy to detect where is the problem. And it gives me the same weird characters – Hassan Sep 07 '21 at 16:57
  • There is a recipe for that on this page https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/ – Andreas Sep 07 '21 at 18:37
  • @Andreas for sorry it gives me the same weird characters! its very strange for me that all the solutions I tried gave me the same result! same garpage! you know even if the excel is empty without no data its also give me same shit – Hassan Sep 07 '21 at 19:00
  • @Andreas when I use the save option only it works fine and xlsx file generates without any problem in the project folder. but when I try to force download it in client side that happens – Hassan Sep 07 '21 at 19:02

3 Answers3

0

You save the spreadsheet to a file, then you read the file, but you dont SEND the file off to the browser all you send are headers with the filename (which is only a suggestion that wil appear in the file save dislog)

try {
    $writer = new Xlsx($spreadsheet);
    $writer->save($filename);
    $content = file_get_contents($filename);
} catch(Exception $e) {
    exit($e->getMessage());
}

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . urlencode($filename) .'"' );

echo $content;  // this actually send the file content to the browser

unlink($filename);

I just ran this exact code and it worked nicely, using the Chrome and Firefox and Edge browsers.

<?php

require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); 


$sheet->setCellValue('A1', 'ID'); 
$sheet->setCellValue('B1', 'Name'); 
$sheet->setCellValue('C1', 'Name2'); 
$sheet->setCellValue('D1', 'Name3');
$sheet->setCellValue('E1', 'Type'); 

// Write an .xlsx file  
$date = date('d-m-y-'.substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$filename = "data/export_".$date.".xlsx";

try {
    $writer = new Xlsx($spreadsheet);
    $writer->save($filename);
    $content = file_get_contents($filename);
} catch(Exception $e) {
    exit($e->getMessage());
}

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . urlencode($filename) .'"' );

echo $content;  // this actually send the file content to the browser

unlink($filename);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • its really disappointing. it gives me the same thing. https://drive.google.com/file/d/1SftItKQWAIWIob3HH-K0etdXKWdagUNO/view?usp=sharing – Hassan Sep 07 '21 at 17:15
  • I cannot actually see that – RiggsFolly Sep 08 '21 at 10:06
  • I really do not know whats going on! all solutions gives me the same thing! same weird characters I show you on the image. all solutions are same thing its not logical at all there must be a problem with something. I tried also to install the phpspreadsheet once more but it also gave me the same – Hassan Sep 08 '21 at 10:22
  • This code is working for me. Thank you very much. – Haizad Annuar Nov 24 '21 at 02:42
  • @Hassan If you are still getting the same result, maybe you are outputting something (anything) before or after you send the headers ?? – AreaEuro Nov 29 '21 at 05:37
0

The following seems to be working:

Note: $filePath should be a folder with the right permissions

<?php 

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); 


$sheet->setCellValue('A1', 'ID'); 
$sheet->setCellValue('B1', 'Name'); 
$sheet->setCellValue('C1', 'Name2'); 
$sheet->setCellValue('D1', 'Name3');
$sheet->setCellValue('E1', 'Type'); 

// Write an .xlsx file  
$date = date('d-m-y-'.substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$filename = "export_".$date.".xlsx";
$filePath = __DIR__ . DIRECTORY_SEPARATOR . $filename; //make sure you set the right permissions and change this to the path you want

try {
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save($filePath);
} catch(Exception $e) {
    exit($e->getMessage());
}

// redirect output to client browser
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');

$writer->save('php://output');
Andreas
  • 5,305
  • 4
  • 41
  • 60
0

Try using this for your header 'Content-Type' -

header('Content-Type: application/vnd.ms-excel; charset=UTF-8');

Truecoz
  • 1
  • 1