dttnb997315 2019-02-03 15:18
浏览 206
已采纳

无法使用phpspreadsheet下载excel文件

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.

  • 写回答

1条回答 默认 最新

  • doujiu9172 2019-02-05 13:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?