<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                author @小凱 [TOC] ## 一、前期準備工作 > 前期準備工作可以看看PHPExcel導入的準備,另外導出需要準備一個數據列表頁面,一張學生信息表。 * * * * * ## 二、具體操作流程及實現思路 **實現思路**: 1.首先將數據庫里面的學生信息取出來,展示在界面。我們將新建一個student方法,來展示學生信息。 2.通過點擊 導出Excel按鈕 請求導出處理函數export。 3.export收到請求后,將取出來的數據循填入到excel表格中。 4.數據填完了,生成excel表格輸出在瀏覽器下載。 * * * * * ## 三、案例展示 **前臺代碼** ~~~ <div class="container"> <table class="table table-bordered" width="60%"> <thead> <tr> <th width="30">ID</th> <th width="50">姓名</th> <th width="30">年齡</th> <th width="30">班級</th> <th width="30">電話</th> <th width="30">郵箱</th> </tr> </thead> <tbody> {volist name="list" id="v"} <tr> <td>{$v['id']}</td> <td>{$v['name']}</td> <td>{$v['age']}</td> <td>{$v['class']}</td> <td>{$v['tel']}</td> <td>{$v['email']}</td> </tr> {/volist} </tbody> </table> <button type="button" onclick="window.open('{:url('export')}')">導出Excel</button> </div> ~~~ * * * * * **后臺代碼** ~~~ //表格導出處理 public function export(){ //1.從數據庫中取出數據 $list = Db::name('student')->select(); //2.加載PHPExcle類庫 vendor('PHPExcel.PHPExcel'); //3.實例化PHPExcel類 $objPHPExcel = new \PHPExcel(); //4.激活當前的sheet表 $objPHPExcel->setActiveSheetIndex(0); //5.設置表格頭(即excel表格的第一行) $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'ID') ->setCellValue('B1', '姓名') ->setCellValue('C1', '年齡') ->setCellValue('D1', '班級') ->setCellValue('E1', '電話') ->setCellValue('F1', '郵箱'); //設置F列水平居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment() ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //設置單元格寬度 $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(15); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30); //6.循環剛取出來的數組,將數據逐一添加到excel表格。 for($i=0;$i<count($list);$i++){ $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['id']);//添加ID $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['name']);//添加姓名 $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['age']);//添加年齡 $objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['class']);//添加班級 $objPHPExcel->getActiveSheet()->setCellValue('E'.($i+2),$list[$i]['tel']);//添加電話 $objPHPExcel->getActiveSheet()->setCellValue('F'.($i+2),$list[$i]['email']);//添加郵箱 } //7.設置保存的Excel表格名稱 $filename = '學生信息'.date('ymd',time()).'.xls'; //8.設置當前激活的sheet表格名稱; $objPHPExcel->getActiveSheet()->setTitle('學生信息'); //9.設置瀏覽器窗口下載表格 header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="'.$filename.'"'); //生成excel文件 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //下載文件在瀏覽器窗口 $objWriter->save('php://output'); exit; } ~~~ * * * * * ## 四、代碼下載地址及更新日期 >說明:每次demo的數據庫文件將放在db目錄下,需要的導入數據庫即可。 **代碼地址[https://github.com/liuzhen153/thinkphp5-demo](https://github.com/liuzhen153/thinkphp5-demo) 的 [PHPExcel-demo2](https://github.com/liuzhen153/thinkphp5-demo/tree/master/PHPExcel-demo2)** > 本次更新:2017/2/18 晚
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看