PHP導出數據到表格的實例


我發現最近的項目須要導出Excel表的頁面很是的多,想來這個也是咱們經常使用的功能了,如今我能夠很熟練的導出了,可是記得當時本身第一次導出時仍是繞了一些彎路的,那麼如今我就來記錄下我此次用exshop框架項目下的導出(其實在不一樣的框架下Excel的導出原理都是差很少的)javascript


前端
<a href="javascript:;" id="export_all" class="coolbg">導出</a>

<script>
    //導出數據
    $('#export_all').click(function(){
        window.open('index.php?app=craft_order&act=export', '_blank');
    });
</script>
控制器
//    導出數據
    public function export() {
        $result = $this->_oaOrderModel->getAllOrderListForManager($this->store_id, $orderSn=null, $buyer_id=null, $buyer_name=null, $consignee=null, $phone=null, $company_name=null, $status=null, $s_time=null, $e_time=null, $page=null, $listRows=null, $execl=true); //這個是得到數據的代碼-model裏
        $orderList = $result['orderList'];
        if (!empty($orderList)) {
            $j = 1;
            $stmt = array();
            foreach ($orderList as $val) {
                $stmt[$j]['網站ID'] = $val['store_id'];
                $stmt[$j]['訂單信息'] = $val['order_sn'];
                $stmt[$j]['商品信息'] = $val['inventory_sn_count_chinese'];
                $stmt[$j]['工藝選擇'] = $val['craft_count_chinese'];
                $stmt[$j]['商品總數量'] = $val['real_goods_total_count'];
                $stmt[$j]['提交日期'] = date("Y-m-d H:i:s",$val['add_time']);
                $stmt[$j]['客戶名稱'] = $val['company_name'];
                $stmt[$j]['聯繫人'] = $val['consignee'];
                $stmt[$j]['聯繫方式'] = $val['phone_mob'];
                $stmt[$j]['訂單完成率'] = $val['percentage_complete'];
                $stmt[$j]['訂單狀態'] = $val['statusChinese'];
                $j++;
            }
            $current_path = dirname(__FILE__);
            $home_path = dirname($current_path);
            require_once ROOT_PATH . '/includes/libraries/PHPExcel.php';
            require_once ROOT_PATH . '/includes/libraries/PHPExcel/IOFactory.php';
            $objPHPExcel = new PHPExcel(); //這個方法本身下載放到公共方法裏
            $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                ->setLastModifiedBy("Maarten Balliauw")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");

            // 行高
            for ($i = 2; $i <= count($stmt); $i++) {
                $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(22);
            }
            foreach ($stmt as $fid => $fval) {
                if ($fid == 1) {
                    $key = 0;
                    foreach ($fval as $title => $first) {
                        //若是一級標題
                        $objPHPExcel->getActiveSheet()->setCellValue(chr($key + 65) . '1', $title);
                        $objPHPExcel->getActiveSheet()->getStyle(chr($key + 65) . '1')->getFont()->setBold(true);       // 加粗
                        $key ++;
                    }
                }
                $cid = 0;
                $row_id = $fid + 1;
                foreach ($fval as $cval) {
                    $objPHPExcel->getActiveSheet()->setCellValue(chr($cid + 65) . (string) ($row_id), $cval);
                    $cid++;
                }
            }
            $objPHPExcel->setActiveSheetIndex(0);
            $objPHPExcel->getActiveSheet()->setTitle('Excel表');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            //$objWriter->save('訂單列表詳細.xls');
            //輸出到瀏覽器
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            header('Content-Disposition:inline;filename="訂單列表.xls"');
            header("Content-Transfer-Encoding: binary");
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Pragma: no-cache");
            $objWriter->save('php://output');
        }
    }
成果圖

clipboard.png

clipboard.png

clipboard.png


心得

有時候遇到這些問題能夠多思考,多看看它的原理,原理理解了下次作其它的也是會的,但最重要的是要懂得作記錄,咱們的記憶並無想象的那麼好php

注:文章來源雨中笑記錄實習期遇到的問題與心得,轉載請申明原文前端

相關文章
相關標籤/搜索