PHP 创建一个 excel 文件并通过一个函数 return 它
PHP create an excel file and return it via a function
我正在研究 yii2
。我正在通过 php spreadsheet
创建一个 excel 文件。现在,我正在浏览器中下载它。现在,我想 return 文件而不下载它。
代码:
// Created 2 worksheet in one excel file successfully
$filename = 'Meter Breakdown List '.date('d-m-y').'.xlsx'; //save our workbook as this file name
// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
之后我想要它 return 像 return $filename;
这样的文件
然后将 returned 文件传递给名为 sendEmail($filename)
的函数
在这个功能中,我已经设置了通过附件发送电子邮件的过程。
如何在不下载文件的情况下发送文件?
注:
我也看了问题:PHP create Excel spreadsheet and then email it as an attachment。
样本code保存文件
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
文件保存后,您可以将文件发送到邮箱
sendEmail('hello world.xlsx');
完成所有操作后,您可以删除保存的文件
unlink('hello world.xlsx');
我正在研究 yii2
。我正在通过 php spreadsheet
创建一个 excel 文件。现在,我正在浏览器中下载它。现在,我想 return 文件而不下载它。
代码:
// Created 2 worksheet in one excel file successfully
$filename = 'Meter Breakdown List '.date('d-m-y').'.xlsx'; //save our workbook as this file name
// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
之后我想要它 return 像 return $filename;
然后将 returned 文件传递给名为 sendEmail($filename)
在这个功能中,我已经设置了通过附件发送电子邮件的过程。
如何在不下载文件的情况下发送文件?
注:
我也看了问题:PHP create Excel spreadsheet and then email it as an attachment。
样本code保存文件
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
文件保存后,您可以将文件发送到邮箱
sendEmail('hello world.xlsx');
完成所有操作后,您可以删除保存的文件
unlink('hello world.xlsx');