phpangularphpexcelphpspreadsheet

Can't download excel file with phpspreadsheet


I use angular 6 with PHP and I want to download an excel file using phpspreadsheet.

When I use the default code

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

the file is generated to the php folder without being downloaded and works fine. Now I would like to download it and select where to save it.

I use the code that I found in the documentation

// redirect output to client browser
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

but the file is not downloaded and I get this in my browser:enter image description here

At the begining of my PHP file I have

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

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

My service in Angular is:

export_excel(localUserid, date, projectid) {
    return this.http.post(this.apiUrl, {localUserid, date, projectid},
    {
      headers : {
          'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
      } 
     })
  }

I pass some values to PHP. I tried to change the headers in Angular but nothing worked. Does anyone knows how to solve it? Thank you.


Solution

  • After trying many different solutions, I found the correct way to download an excel file using Angular 6 and PHPSpreadsheet.

    First of all in PHP I had to save the file locally:

    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $file_name = 'D:\wamp64\www\angular6-app\client\download_excel\hello_world.xlsx'; //it depends where you want to save the excel
    $writer->save($file_name);
    if(file_exists($file_name)){
        echo json_encode(array('error'=>false, 'export_path'=>'download_excel/' . 'hello_world.xlsx')); //my angular project is at D:\wamp64\www\angular6-app\client\
    }
    

    Then, I changed my excel.service.ts:

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http'
    
    interface excel {
      error: any,
      export_path: any
    }
    
    @Injectable({
      providedIn: 'root'
    })
    export class ExcelService {
    
      apiUrl = "http://localhost/angular6-app/api/exportexcel.php";
    
      constructor(private http: HttpClient) { }
    
      export_excel(localUserid, date, projectid) {
        return this.http.post<excel>(this.apiUrl, {localUserid, date, projectid},
        {
          headers : {
              'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
          } 
         })
      }
    }
    

    And finally in my angular component I redirect the user to my export path if everything worked correctly:

    this.srv.export_excel(localUserid, date, projectid).subscribe((data) => 
    {  
        location.href = data.export_path;
    }
    

    Now I have the excel downloaded in my downloads folder.