The simplest way to read a spreadsheet is using IOFactory::load(), which automatically detects the file format:
<?phpuse PhpOffice\PhpSpreadsheet\IOFactory;require 'vendor/autoload.php';$inputFileName = 'example.xlsx';// Load the file - IOFactory automatically detects the format$spreadsheet = IOFactory::load($inputFileName);// Get data from the active sheet as an array$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);// Display the dataprint_r($sheetData);
For better performance, you can read only the cell values without loading formatting, styles, or other metadata:
<?phpuse PhpOffice\PhpSpreadsheet\IOFactory;require 'vendor/autoload.php';$inputFileName = 'example.xlsx';// Create reader and set to read data only$reader = IOFactory::createReader('Xlsx');$reader->setReadDataOnly(true);// Load the spreadsheet$spreadsheet = $reader->load($inputFileName);
<?phpuse PhpOffice\PhpSpreadsheet\IOFactory;require 'vendor/autoload.php';$spreadsheet = IOFactory::load('example.xlsx');// Get all worksheet names$worksheetNames = $spreadsheet->getSheetNames();echo "Worksheets in this file:\n";foreach ($worksheetNames as $name) { echo "- " . $name . "\n";}// Access specific worksheet by name$sheet = $spreadsheet->getSheetByName('Sheet2');// Or by index (0-based)$firstSheet = $spreadsheet->getSheet(0);// Get data from specific sheet$sheetData = $sheet->toArray();