這篇文章給大家分享的是有關(guān)TP5如何引用PHPExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、陽信網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為陽信等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。PHPExcel
將下載好的PHPExcel文件夾跟PHPExcel放置在框架中的Vendor目錄下,注意目錄的結(jié)構(gòu),按照我的代碼來基本上直接引用就能使用
前端代碼
<html> <head></head> <body> <div class="panel-heading"> Advanced Tables <a href="/daochu" class="btn-succes">導(dǎo)出</a> <form action="/daoru" method="post" enctype="multipart/form-data"> <input name="upload[]" type="file" /> <input type="submit" /> </form> </div> </body> </html>
導(dǎo)出功能控制器的代碼
$list = Db::table('product')->select(); vendor("PHPExcel176.PHPExcel"); $objPHPExcel = new \PHPExcel(); $objPHPExcel->getProperties()->setCreator("ctos") ->setLastModifiedBy("ctos") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50); //設(shè)置行高度 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22); $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20); //set font size bold $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10); $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN); //設(shè)置水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //合并cell $objPHPExcel->getActiveSheet()->mergeCells('A1:J1'); // set table header content $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '訂單數(shù)據(jù)匯總 時(shí)間:'.date('Y-m-d H:i:s')) ->setCellValue('A2', '訂單ID') ->setCellValue('B2', '商品名稱') ->setCellValue('C2', '價(jià)格') ->setCellValue('D2', '庫存') ->setCellValue('E2', '圖片'); // Miscellaneous glyphs, UTF-8 for($i=0;$i<count($list)-1;$i++){ $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $list[$i]['id']); $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $list[$i]['name']); $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $list[$i]['price']); $objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), $list[$i]['stock']); $objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+3), $list[$i]['main_img_url']); //$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':J'.($i+3))->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':J'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16); } // sheet命名 $objPHPExcel->getActiveSheet()->setTitle('訂單匯總表'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // excel頭參數(shù) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="商品表('.date('Ymd-His').').xls"'); //日期為文件名后綴 header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel5為xls格式,excel2007為xlsx格式 $objWriter->save('php://output');
導(dǎo)入功能控制器的代碼
public function daoru(){ $file = $_FILES['upload']['tmp_name'][0]; $data = $this->import_excel($file); var_dump($data); } private function import_excel($file){ // 判斷文件是什么格式 $type = pathinfo($file); $type = strtolower($type["extension"]); $type=$type==='csv' ? $type : 'Excel5'; ini_set('max_execution_time', '0'); Vendor('PHPExcel176.PHPExcel'); // 判斷使用哪種格式 $objReader = \PHPExcel_IOFactory::createReader($type); $objPHPExcel = $objReader->load($file); $sheet = $objPHPExcel->getSheet(0); // 取得總行數(shù) $highestRow = $sheet->getHighestRow(); // 取得總列數(shù) $highestColumn = $sheet->getHighestColumn(); //循環(huán)讀取excel文件,讀取一條,插入一條 $data=array(); //從第一行開始讀取數(shù)據(jù) for($j=3;$j<=$highestRow;$j++){ //從A列讀取數(shù)據(jù) for($k='A';$k<=$highestColumn;$k++){ // 讀取單元格 $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue(); } } return $data; }
這里獲得excel文件的數(shù)據(jù)內(nèi)容,可以循環(huán)插入數(shù)據(jù)庫中
感謝各位的閱讀!關(guān)于“TP5如何引用PHPExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁標(biāo)題:TP5如何引用PHPExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能-創(chuàng)新互聯(lián)
文章URL:http://jinyejixie.com/article28/ccchjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站策劃、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容