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');
?>