TP3.2中phpexcel匯入excel方法分享

來源:互聯網
上載者:User
本文主要和大家分享TP3.2中phpexcel匯入excel方法,希望能協助到大家。







具體代碼:

Vendor('PHPExcel.PHPExcel');public function excel_runimport(){    $PHPExcel=new \PHPExcel();    if (! empty ( $_FILES ['file'] ['name'] )){        $file_types = explode ( ".", $_FILES ['file'] ['name'] );        $exts = $file_types [count ( $file_types ) - 1];        /*判別是不是.xls檔案,判別是不是excel檔案*/        if (strtolower ( $exts ) != "xlsx" || strtolower ( $exts ) != "xls"){                $this->error ( '不是Excel檔案,重新上傳');        }        //產生唯一的ID $filename = md5(uniqid(microtime(true),true));        $config=array('maxSize'=>70000000,                'exts'=>array('xlsx','xls'),                'rootPath'=>'./Uploads/excel/',                //儲存的檔案名稱                'saveName' =>$filename,                //開啟子目錄                'subName' =>array('date','Ymd'),        );        $upload=new \Think\Upload($config);        $info=$upload->upload();        if($info){if($exts == 'xls'){            import("Vendor.PHPExcel.Reader.Excel5");            $PHPReader=new \PHPExcel_Reader_Excel5();        }else if($exts == 'xlsx'){            import("Vendor.PHPExcel.Reader.Excel2007");            $PHPReader=new \PHPExcel_Reader_Excel2007();        }        $rootPath='./Uploads/excel/';$savePath = $info['file']['savepath'];        $saveName=$info['file']['savename'];         //負載檔案建立對象        $filePath=$rootPath.$savePath.$saveName;$objReader = $PHPReader->load($filePath);        //擷取表中的第一個工作表,如果要擷取第二個,把0改為1,依次類推        $currentSheet=$objReader->getSheet(0);        //擷取總列數        $allColumn=$currentSheet->getHighestColumn();        //擷取總行數        $allRow=$currentSheet->getHighestRow();        //迴圈擷取表中的資料,$currentRow表示當前行,從哪行開始讀取資料,索引值從0開始        $data = array();//建立空數組接收表格式資料        //從第幾行開始迴圈        for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++){            //迴圈讀取每個儲存格的內容。注意行從1開始,列從A開始            //迴圈列                for($colIndex='A';$colIndex<=$allColumn;$colIndex++){                        $addr = $colIndex.$rowIndex;                        $cell = $currentSheet->getCell($addr)->getValue();                        if($cell instanceof PHPExcel_RichText){                            //富文本轉換字串                            $cell = $cell->__toString();                        }                    $data[$rowIndex][$colIndex] = $cell;            }        }            if(is_file($filename)) unlink($filename);            }else{                echo $upload->getError();            }                // $this->success ('匯入資料庫成功',U('excel_import'),1);    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.