PHP读取Excel表格数据

读取Excel表格数据需要用到PHPExcel插件

 

<?php
/**
 * Created by PhpStorm.
 * User: zhangdan
 * Date: 2018/3/27
 * Time: 17:23
 */
include 'D:\xampp\htdocs\PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';    //这里引入PHPExcel插件

class TestController extends CController
{
    public $layout = '//layouts/test';
    public $navData = array();

    public function actionGetXXX()
    {

        $PHPReader = new \PHPExcel_Reader_Excel2007();
        $PHPExcel = $PHPReader->load('ExcelPath');  //可以是绝对路径
        /**读取excel文件中的第一个工作表*/

        $currentSheet = $PHPExcel->getSheet(0);
        /**取得最大的列号*/

        $allColumn = $currentSheet->getHighestColumn();
        /**取得一共有多少行*/

        $allRow = $currentSheet->getHighestRow();

        $date = [];
        /**从第1行开始输出*/
        for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {

            /**从第A列开始输出*/
            for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();
                /**ord()将字符转为十进制数*/
                $date[$currentRow - 1][] = $val;
            }

        }
    return data;
  }
}

下载PHPExcel插件地址:https://github.com/PHPOffice/PHPExcel

posted @ 2018-03-28 11:00  ʚ一只咩ɞ  阅读(553)  评论(0编辑  收藏  举报