PhpSpreadsheet (Excel / CSV Read & Write)
The artiframe add spreadsheet command installs the phpoffice/phpspreadsheet package, which is the undisputed industry standard for generating and reading Excel (.xlsx), CSV, and ODS files in PHP.
It is a lifesaver when exporting database dumps to Excel or handling bulk product imports.
💡 Information: Direct Usage
ArtiFrame does not copy a specific integration (Wrapper/Stub) class to the
ArtiFrame does not copy a specific integration (Wrapper/Stub) class to the
src/Service folder for this package. The installation simply includes the package into the project via Composer.
The codes below are basic usage examples demonstrating the package's native operations. For more information, please review the PhpSpreadsheet Documentation.
Installation
terminal
$ artiframe add spreadsheet
Usage Example: Creating and Downloading Excel (Export)
Let's instantly convert a user list fetched from the database into an Excel file and force the browser to download it.
ExportController.php
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// 1. Create a new workbook
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 2. Write Column Headers
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', 'Full Name');
$sheet->setCellValue('C1', 'Email');
// Make headers Bold
$sheet->getStyle('A1:C1')->getFont()->setBold(true);
// 3. Write Data (Normally comes from Database)
$sheet->setCellValue('A2', '1');
$sheet->setCellValue('B2', 'John Doe');
$sheet->setCellValue('C2', '[email protected]');
$sheet->setCellValue('A3', '2');
$sheet->setCellValue('B3', 'Jane Smith');
$sheet->setCellValue('C3', '[email protected]');
// 4. Prepare the writer for Excel (Xlsx) format
$writer = new Xlsx($spreadsheet);
// 5. Set HTTP Headers to instruct the browser: "This is an Excel file, Download it!"
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Users.xlsx"');
header('Cache-Control: max-age=0');
// Send the file to the browser's output stream (php://output)
$writer->save('php://output');
exit;
Usage Example: Reading Excel (Import)
ImportController.php
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
// Specify the path of the uploaded file
$inputFileName = '/tmp/products.xlsx';
// Load the file into memory
$spreadsheet = IOFactory::load($inputFileName);
// Convert all data in the active sheet into a standard PHP Array
$data = $spreadsheet->getActiveSheet()->toArray();
foreach ($data as $row) {
echo "Product: " . $row[0] . " | Price: " . $row[1] . "<br>";
}