Η ανάγνωση αρχείων Excel (.xls, .xlsx) στο PHP γίνεται μέσω βιβλιοθηκών. Η παλιά PHP-ExcelReader υποστηρίζει μόνο .xls (BIFF format). Για σύγχρονα projects, χρησιμοποιήστε PhpSpreadsheet (successor του PHPExcel) που υποστηρίζει και τα δύο formats.
composer require phpoffice/phpspreadsheet
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$filePath = 'spreadsheet.xlsx'; // ή .xls
try {
// Αυτόματη ανίχνευση format
$spreadsheet = IOFactory::load($filePath);
$sheet = $spreadsheet->getActiveSheet();
// Μετατροπή σε array
$data = $sheet->toArray(null, true, true, true);
// Εμφάνιση
echo "<table border='1'>";
foreach ($data as $row) {
echo "<tr>";
foreach ($row as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>";
}
echo "</table>";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>
<?php
$spreadsheet = IOFactory::load('spreadsheet.xlsx');
$sheet = $spreadsheet->getActiveSheet();
// Αξία κελιού A1
$value = $sheet->getCell('A1')->getValue();
// Αξία με coordinates
$value = $sheet->getCellByColumnAndRow(1, 1)->getValue(); // A1
// Formatted value (ως φαίνεται στο Excel)
$formatted = $sheet->getCell('A1')->getFormattedValue();
// Iterate rows
foreach ($sheet->getRowIterator() as $row) {
$rowIndex = $row->getRowIndex();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Κενά κελιά
foreach ($cellIterator as $cell) {
echo "Cell {$cell->getCoordinate()}: " . $cell->getValue() . "\n";
}
}
?>
<?php
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
$reader = new Xlsx(); // ή Xls() για .xls
$reader->setReadDataOnly(true); // Μόνο δεδομένα, όχι formatting
// Χρήση chunk reading γιαα αρχεία
$reader->setReadFilter(new \PhpOffice\PhpSpreadsheet\Reader\ChunkReadFilter(1, 1000)); // Rows 1-1000
$spreadsheet = $reader->load('large_file.xlsx');
// Process chunk...
?>
Δεν συντηρείται. Μόνο για πολύ παλιά projects.
<?php
require 'excel_reader2.php'; // Από το πακέτο PHP-ExcelReader
$data = new Spreadsheet_Excel_Reader('file.xls', false, 'UTF-8');
echo "<table>";
for ($row = 1; $row <= $data->rowcount(); $row++) {
echo "<tr>";
for ($col = 1; $col <= $data->colcount(); $col++) {
echo "<td>" . htmlspecialchars($data->val($row, $col)) . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
| Χαρακτηριστικό | PhpSpreadsheet | PHP-ExcelReader |
|---|---|---|
| .xls support | Ναι | Ναι |
| .xlsx support | Ναι | Όχι |
| Active maintenance | Ναι | Όχι (από 2010+) |
| Formulas | Ναι (calculation engine) | Όχι |
| Formatting/Styles | Πλήρης υποστήριξη | Μινιμαλ |
| Memory efficiency | Chunk reading, read filters | Φορτώνει όλο το αρχείο |
| Composer | Ναι | Όχι |
<?php
// Για CSV - built-in functions
$handle = fopen('data.csv', 'r');
while (($row = fgetcsv($handle, 1000, ',')) !== false) {
// $row είναι array
print_r($row);
}
fclose($handle);
// League\Csv (advanced CSV)
composer require league/csv
setReadDataOnly(true) αν δεν χρειάζεστε formattinghtmlspecialchars() για web displayΠνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.