雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

phpoffice/phpexcel 读取Excel表格数据

2024-09-08 10

1. 使用示例


TP5.0

$file = request()->file('file');$data = Excel::read($file->getRealpath());

2. 封装类


<?php/** * 导入数据 * composer require phpoffice/phpexcel * PHP7.2版本以下推荐使用 phpoffice/phpexcel * PHP7.2版本以上推荐使用 phpoffice/phpspreadsheet */class Excel{    /**     * 读取表格数据     * @param string 临时文件路径     * @return array     */    public static function read($file)    {        // 设置excel格式        $reader = PHPExcel_IOFactory::createReader('Excel5');        // 载入excel文件        $excel = $reader->load($file);        // 读取第一张表        $sheet = $excel->getSheet(0);        // 获取总行数        $row_num = $sheet->getHighestRow();        // 获取总列数        $col_num = $sheet->getHighestColumn();        $data = []; //数组形式获取表格数据        for ($col = 'A'; $col <= $col_num; $col++) {            for ($row = 2; $row <= $row_num; $row++) {                $data[$row - 2][] = $sheet->getCell($col . $row)->getValue();            }        }        return $data;    }}
更新于:8天前
赞一波!

文章评论

全部评论