thinkphp5.1框架導(dǎo)入excel表

描述:下面import()方法是將上傳的excel表轉(zhuǎn)為一個二維數(shù)組,必須傳入兩個參數(shù),參數(shù)1file為文件名,參數(shù)2file_tmp為臨時目錄文件,我的excel表格第一行是描述,直接在方法中過濾掉。

1.要使用方法,首先引入類庫,我用的是phpoffice
2.composer引入方法:項目中執(zhí)行 composer require phpoffice/phpexcel
public function import($file,$file_tmp)
    {
        //獲取文件后綴
        $extension = strtolower( pathinfo($file, PATHINFO_EXTENSION) );
        //定義可上次的文件后綴
        $extensions = ['xlsx','xls'];
        //判斷文件后綴是否合法
        if(!in_array($extension,$extensions)){
            return '上傳文件不合法';
        }
        $spreadsheet = IOFactory::load($file_tmp);
        $sheet = $spreadsheet->getActiveSheet();
    //        $data = $sheet->getCellCollection ();
    //        $title = $sheet->getTitle();
    //        $data = $sheet->getColumnDimensions();
        $highestRow = $sheet->getHighestRow(); // 總行數(shù)
        $highestColumn = $sheet->getHighestColumn(); // 總列數(shù)
        $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);

        $lines = $highestRow - 1;
        if ($lines <= 0) {
            return '表格為空';
        }

        for ($row = 2; $row <= $highestRow; ++$row) {
            $rowData = [];
            for ($column = 1; $column <= $highestColumnIndex; $column++) {
                $model_no = $sheet->getCellByColumnAndRow($column, $row)->getValue();
                $rowData[] = $model_no;
            }
            $rowData1[] = $rowData;
        }

        return $rowData1;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容