What are some common methods for reading and exporting Excel files in PHP?
Reading and exporting Excel files in PHP can be achieved using libraries such as PhpSpreadsheet or PHPExcel. These libraries provide functions to read data from Excel files and export data to Excel files in various formats. Example PHP code snippet using PhpSpreadsheet to read an Excel file and export data to a new Excel file:
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
// Read Excel file
$spreadsheet = IOFactory::load('example.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$data = $worksheet->toArray();
// Export data to a new Excel file
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray($data);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('exported_data.xlsx');
?>
Related Questions
- In the context of PHP, how can regular expressions be utilized to manipulate strings according to specific patterns?
- How can the character encoding mismatch between the MySQL database and PHP application lead to display issues?
- What are the advantages and disadvantages of using regular expressions for word replacement in PHP forum posts?