How to show an Excel with PhpSpreadsheet when loading the information

0

Currently I'm using the PHPSpreadsheet library to take values from PHP to Excel, what it does now is to take the values to an excel and store them in the address that I specify. but what I'm looking for is that when I upload information, show me the excel to open. When I used the PHPExcel if I could, and it was done with: $ objWriter-> save ('php: // output') then the excel was displayed to open. My code with PHPSpreadsheet is the following:

<?php 
require_once("vendor/autoload.php"); 

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

 $spreadsheet = new Spreadsheet();
 $sheet = $spreadsheet->getActiveSheet();
 $a="we";
 $a1="are";
 $a2="the";
 $a3="world"; 

  $sheet->setCellValue('A2', $a);
  $sheet->setCellValue('c2', $a1);
  $sheet->setCellValue('E2', $a2);
  $sheet->setCellValue('G2', $a3);

  $writer = new Xlsx($spreadsheet);
  $writer->save('C:\xampp\ejemplo.xlsx');

 ?>

Could it be done?

    
asked by Kevincs7 10.10.2018 в 21:58
source

1 answer

0

If you can do it in the way you are proposing it with some changes; In this way, create and download the file. Depending on how you have configured the downloads in your browser, you will download it directly or ask for the route in which you want to save it.     

    require_once("vendor/autoload.php"); 

    use PhpOffice\PhpSpreadsheet\Spreadsheet;

    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();

    $a="we";

    $a1="are";

    $a2="the";

    $a3="world"; 


    $sheet->setCellValue('A2', $a);

    $sheet->setCellValue('C2', $a1);

    $sheet->setCellValue('E2', $a2);

    $sheet->setCellValue('G2', $a3);

    $writer = new Xlsx($spreadsheet);


   try {

      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment; filename="file.xls"');
      $writer->save("php://output");
  }
    catch(Exception $e){

        echo $e->getMessage();
    }

?>
    
answered by 17.10.2018 в 15:06