首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP ExcelReader 讀取 Excel 檔案並輸出 HTML 表格

     編輯:PHP 相關知識

前置準備

  1. 下載 PHPExcelReader(或現代 PhpSpreadsheet)。
  2. 引入 reader.php。

核心程式碼

<?php
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1252'); // 或 UTF-8
$data->read('example.xls');

echo '<table border="1">';
for ($sheet=0; $sheetsheets); $sheet++) {
    $rows = $data->sheets[$sheet]['numRows'];
    $cols = $data->sheets[$sheet]['numCols'];
    for ($r=1; $r<=$rows; $r++) {
        echo '<tr>';
        for ($c=1; $c<=$cols; $c++) {
            $val = htmlspecialchars($data->sheets[$sheet]['cells'][$r][$c] ?? '');
            echo '<td>' . nl2br($val) . '</td>';
        }
        echo '</tr>';
    }
}
echo '</table>';
?>

進階:多工作表分頁顯示

原始碼已包含 JavaScript 分頁切換邏輯(change_tabs()),可直接套用。

遷移建議:PhpSpreadsheet

composer require phpoffice/phpspreadsheet
// 讀取 .xlsx/.xls/.csv 統一介面
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('file.xlsx');
$sheet = $spreadsheet->getActiveSheet();
foreach ($sheet->getRowIterator() as $row) {
    $cells = [];
    foreach ($row->getCellIterator() as $cell) $cells[] = $cell->getValue();
    print_r($cells);
}

PhpSpreadsheet 支援新版 Excel 格式、記憶體優化、樣式讀寫、公式計算,是目前維護狀態最好的庫。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved