Example sharing of table generation in phpexcel in tp5

小云云
Release: 2023-03-21 06:24:02
Original
2822 people have browsed it

In recent projects, you need to use phpexcel to generate tables, but when exporting excel from a web page, the suffix of the file will always be html. After debugging for a long time, I found that I need to use ob_end_clean() to clear the contents of the php buffer before the header is output. Because before the herder header is output, php cannot have any output, even a space. Once there is output, the php header you set will be invalid, because the header information at this time has already been fixed.

The problem of garbled characters when outputting excel is also solved in this way.

 /**
     * 下载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");
    }
Copy after login

Related recommendations:

How to solve the error reported by phpexcel in Linux system

Share the method of importing excel from phpexcel in TP3.2

How PHPExcel merges and splits cells

The above is the detailed content of Example sharing of table generation in phpexcel in tp5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template