我在laravel中使用phpspreadsheet包来生成excel文件,到目前为止,它的工作原理就像魅力。这是我的问题,我希望将excel保存在某些目录中而不是弹出式下载中,我给出了一个路径以保存在某些目录中,并且工作正常。
$writer->save($full_path.$filename);
但是仍然弹出弹出窗口,下面是生成excel的代码:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Aligment;
use SpreadsheetReader;
public function export()
{
$spreadsheet = new Spreadsheet();
// Add some data
$sheet = $spreadsheet->getActiveSheet();
$sheet->getStyle('C')->getAlignment()->setHorizontal('left');
$sheet->getColumnDimension('A')->setWidth(20);
$sheet->getColumnDimension('B')->setWidth(20);
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'Service');
$users = User::get();
if(count($users) > 0) {
$rowCount = 2;
foreach($users as $user) {
$sheet->setCellValue('A' . $rowCount, $user->name);
$sheet->setCellValue('B' . $rowCount, $user->service);
$rowCount++;
}
}
$spreadsheet->getActiveSheet()->setTitle('User Services');
$spreadsheet->setActiveSheetIndex(0);
$filename = "file-".date('YmdHis').".xlsx";
$full_path = public_path().'\rpts\\';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
//$writer->save('php://output');
$writer->save($full_path.$filename);
exit;
}
在导出过程中是否禁止save as
弹出窗口?