Creating a workbook in memory and opeing it in excel #3825
-
Hi, I am trying to create an .xlsx workbook with multiple spreadsheets in memory, write to cells and then open it in my Excel application. While this library looks quite robust, the documentation is confusing and difficult to follow. I apologize beforehand to the author but I do not find the documentation user friendly as it is exceedingly difficult to find what I want, including examples (maybe it is there). Can someone suggest some simple examples? If this is impossible and I must first create a temporary file on the server, this is fine as well. Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I don't believe it's possible to have it open in Excel automatically, but you can output it in the browser after the script runs if you would like. or if not you can just save it to a directory if needed. <?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Create a new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Get the active worksheet
$worksheet = $spreadsheet->getActiveSheet();
// Fill in some cells with "Hello World"
$worksheet->setCellValue('A1', 'Hello');
$worksheet->setCellValue('B1', 'World');
// Output the Excel file to the browser
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="example.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
/** Optional: Save the workbook to a specific file location
* $writer = new Xlsx($spreadsheet);
* $filename = 'path/to/your/file.xlsx';
* $writer->save($filename);
* echo "File saved to: $filename";
*/ |
Beta Was this translation helpful? Give feedback.
-
No update in 6 months, no reason to think suggested answer is not correct. Marking it as answer. |
Beta Was this translation helpful? Give feedback.
I don't believe it's possible to have it open in Excel automatically, but you can output it in the browser after the script runs if you would like. or if not you can just save it to a directory if needed.