2

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.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Abduhafiz
  • 3,318
  • 5
  • 38
  • 48

1 Answers1

3

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • error's message in russian: **"Не удается открыть файл "01simple.xlsx", так как формат или расширение этого файла являются недопустимыми. Убедитесь, что файл не поврежден и расширение его имени соответствует его формату."** . In english translated by Google translater: **"Can not open file "01simple.xlsx", as the format or extension of this file are not valid. Make sure that the file is not corrupted and the expansion of its name match its format."** – Abduhafiz Dec 19 '13 at 11:25