tp5中phpexcel產生表格執行個體分享

來源:互聯網
上載者:User
最近項目中需要使用使用phpexcel產生表格,但是在由網頁匯出excel時,檔案的尾碼總是會帶上html。後來調試了半天發現,需要在header頭輸出之前使用ob_end_clean( )去清除php緩衝區中的內容。因為在herder頭輸出之前,php是不能有任何輸出的,哪怕是一個空格,一旦有了輸出,你設定的php header頭就無效了,因為此時的header頭資訊早已經固定。

輸出excle時出現亂碼的情況也是用這種方式解決。

 /**     * 下載excel表格     */    public function down_excel()    {        $params = $_GET;        $filename = $params['cname'] . '收銀明細列表' . date('Y-m-d');        $objPHPExcel = new \PHPExcel();        //設定表頭資訊        $letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L');        $excel_column = array(            'id' => "訂單號",            'dt' => "交易時間",            'money' => "金額",            'status' => "交易狀態",            'pay_type' => "支付渠道",            'device' => "終端號",            'operator' => "收銀員",            'pay_id' => "渠道流水號",            'client_name' => "商戶名稱",            'pay_scense' => "支付情境",            'operate_fee' => "手續約",            'no' => "小票單號",        );        $new_letter = [];        $new_excel_column = [];        foreach ($params['act_name'] as $k => $d) {            $new_letter[$d] = $letter[$k];            $new_excel_column[$d] = $excel_column[$d];        }        foreach ($new_excel_column as $k => $d) {            //設定行寬自動調整            $objPHPExcel->getActiveSheet()->getColumnDimension($new_letter[$k])->setWidth(12);            $objPHPExcel->getActiveSheet()->setCellValue($new_letter[$k] . '1', $d);        }        //填充表格資訊        $data['rows'] = [];        foreach ($params['down_class'] as $item) {            switch ($item) {                case 'pay_success':                    $params['dm_status'] = 2;                    break;                case 'refund_success':                    $params['dm_status'] = 4;                    break;                case 'pay_fail':                    $params['dm_status'] = 3;                    break;                case 'pay_undefined':                    $params['dm_status'] = 5;                    break;            }            $ret = $this->getData($params);            $data['rows'] = array_merge($data['rows'], $ret['rows']);        }        $total = 0;        if (!empty($data['rows'])) {            foreach ($data['rows'] as $k => $d) {                $i = $k + 2;                foreach ($new_letter as $key => $item) {                    //填充資料                    $objPHPExcel->getActiveSheet()->setCellValue($item . $i, strip_tags($d[$key]));                }                $total += strip_tags($d['money']);            }        }        $index = count($data['rows']) + 2;        $objPHPExcel->getActiveSheet()->mergeCells("A{$index}:B{$index}");        $objPHPExcel->getActiveSheet()->setCellValue('A' . $index, '合計');        $objPHPExcel->getActiveSheet()->setCellValue('C' . $index, $total);        //儲存表格版本格式        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");        //下載表格        ob_end_clean();    //關閉緩衝區之後再輸出header頭,在header設定之前,可能某個地方有了輸出,導致Content-Type的類型為text/html,所以輸出的表格尾碼才會是html        header('Content-Type: application/octet-stream');        header("Content-Disposition: attachment; filename=\"{$filename}.xlsx\"");        $objWriter->save("php://output");    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.