Examples of PHP export data to a spreadsheet

I recently discovered the need to export the project page Excel table very much, this is what we wanted to commonly used features, and now I can derive a very skilled, but remember that when his first export or around a few detours, then now I come to the next record I used to export under the framework of exshop project (in fact exported under a different framework of the principle of Excel are similar)

front end
 <a href="javascript:;" id="export_all" class="coolbg">导出</a>

        window.open('index.php?app=craft_order&act=export', '_blank');
//    导出数据
    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'];
            $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++) {
            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);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            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");
Results Figure





Sometimes encounter these problems can be more thinking, more to see how it works, do other principles to understand the next time also will be, but the most important thing is to know how to take notes, our memory is not as good as imagined

Note: Source the rain laughing recording internship encountered problems and experiences, reprint please state the original

Guess you like

Origin www.cnblogs.com/homehtml/p/11850934.html