title: how to download Xlsx file using PhpSpreadSheet to client
body: <p>I am trying to download &quot;Xlsx&quot; file using PhpSpreadSheet and I am sure code is working fine but all I get is these strange characters shows up in my console<a href="https://i.stack.imgur.com/q9KHz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q9KHz.png" alt="enter image description here" /></a></p>
  <p>I tried nearly all the solutions here in stackoverflow but all give me the same result below in the image</p>
  <p>solutions I have tried:
  <a href="https://stackoverflow.com/questions/58971049/phpspreadsheet-download-file-instead-of-saving-it">solution</a>
  <a href="https://stackoverflow.com/questions/50319493/phpspreadsheet-return-file-instead-of-saving-it">more solutions</a></p>
  <p>my code</p>
  <pre><code>&lt;?php
  
  
  require 'vendor/autoload.php';
  use PhpOffice\PhpSpreadsheet\Spreadsheet;
  use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  
  $spreadsheet = new Spreadsheet();
  $sheet = $spreadsheet-&gt;getActiveSheet(); 
  
  
  $sheet-&gt;setCellValue('A1', 'ID'); 
  $sheet-&gt;setCellValue('B1', 'Name'); 
  $sheet-&gt;setCellValue('C1', 'Name2'); 
  $sheet-&gt;setCellValue('D1', 'Name3');
   $sheet-&gt;setCellValue('E1', 'Type'); 
  
  // Write an .xlsx file  
  $date = date('d-m-y-'.substr((string)microtime(), 1, 8));
  $date = str_replace(&quot;.&quot;, &quot;&quot;, $date);
  $filename = &quot;export_&quot;.$date.&quot;.xlsx&quot;;
  
  try {
  $writer = new Xlsx($spreadsheet);
      $writer-&gt;save($filename);
      $content = file_get_contents($filename);
  } catch(Exception $e) {
      exit($e-&gt;getMessage());
  }
  
  header(&quot;Content-Disposition: attachment; filename=&quot;.$filename);
  
  unlink($filename);
  </code></pre>