Abduhafiz
Abduhafiz

Reputation: 3404

Generate Excel file, but do not save to the server

I have an Excel file file.xlsx. I open this file with PHPExcel and insert my datas:

$excel2->getActiveSheet()
                    ->setCellValue('A2', $rowCardCode['podpodrazd'].", ".$rowCardCode['schet'])
                    ->setCellValue('C2', $rowCardCode['tovar_name'])
                    ->setCellValue('C3', $rowCardCode['edinica_izmer']);

After, I can save generated file with new name (temp/New_file.xlsx).

$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('files/temp_file/card/card_'.time().'.xlsx');

I don't want to save generated file to the server. I just want generate Excel file and give it to user for downloading.

Is it possible to do?

Thanks.

Upvotes: 2

Views: 4715

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

The 01simple-download-xlsx.php example in the PHPExcel /Examples folder shows exactly how to do this

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('php://output');
exit;

instead of

$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('files/temp_file/card/card_'.time().'.xlsx');

But make sure that you're not outputting anything else at all to the browser

Upvotes: 3

Related Questions