PHP export data to table instance

Keywords: PHP Excel Javascript encoding

I found that there are many pages to export excel table for recent projects. I think this is also our common function. Now I can export it skillfully, but I remember that when I first exported it, there were some detours, so now I will record my export under the project of exhop framework (in fact, the export principles of Excel under different frameworks are all the same Almost)

Front end

 <a href="javascript:;" id="export_all" class="coolbg">export</a>

<script>
    //Derived data
    $('#export_all').click(function(){
        window.open('index.php?app=craft_order&act=export', '_blank');
    });
</script>

Controller
```
// Derived data
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); //This is the code to get data - model
$orderList = $result['orderList'];
if (!empty($orderList)) {
$j = 1;
$stmt = array();
foreach ($orderList as $val) {
$stmt[$j]['website ID'] = $val['store_id'];
$stmt[$j]['Order information'] = $val['order_sn'];
$stmt[$j]['Commodity information'] = $val['inventory_sn_count_chinese'];
$stmt[$j]['Process selection'] = $val['craft_count_chinese'];
$stmt[$j]['Total quantity of goods'] = $val['real_goods_total_count'];
$stmt[$j]['Date of submission'] = date("Y-m-d H:i:s",$val['add_time']);
$stmt[$j]['Customer name'] = $val['company_name'];
$stmt[$j]['Contacts'] = $val['consignee'];
$stmt[$j]['Contact information'] = $val['phone_mob'];
$stmt[$j]['Order fulfillment rate'] = $val['percentage_complete'];
$stmt[$j]['Order status'] = $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(); //This method can be downloaded and put into the public method
$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");

        // Row height
        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) {
                    //If the first level title
                    $objPHPExcel->getActiveSheet()->setCellValue(chr($key + 65) . '1', $title);
                    $objPHPExcel->getActiveSheet()->getStyle(chr($key + 65) . '1')->getFont()->setBold(true);       // Thickening
                    $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 surface');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //$objwriter - > Save ('order list details. xls');
        //Output to browser
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="Order list.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');
    }
}

```

Note: PHPExcel(); / / download this method and put it into the public method

Result map

Experience

Sometimes we can think more about these problems and look at its principle. We understand that we will do other things next time. But the most important thing is to know how to record. Our memory is not as good as we think

Note: the source of the article is yuzhongxiao, who records the problems and experience during the internship. Please state the original

Posted by MeanMrMustard on Mon, 06 Jan 2020 02:33:29 -0800