How to export Excel in php without plug-ins and enabling configuration? (code)

不言
Release: 2023-04-03 21:50:01
Original
1423 people have browsed it

The content of this article is about how to export Excel in PHP without plug-ins and enabling configuration? (code), it has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

function export_csv($filename, $data)
    {
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=" . $filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $data;
    }


//csv导出
    /*
     * 导出Excel
     */
    public function output()
    {
        $org         = Db::name('organization')->select();
        $list['one'] = Db::name('transfer_flow')->order('add_time desc')->select();
        foreach ($list['one'] as $k => $v) {
            foreach ($org as $kk => $vv) {
                if ($v['org_id'] == $vv['id']) {
                    $list['one'][$k]['org_id'] = $vv['name'];
                }
            }
            if ($v['pay_type'] === 'WX') {
                $list['one'][$k]['pay_type'] = '微信';
            } else if ($v['pay_type'] === 'A') {
                $list['one'][$k]['pay_type'] = '支付宝';
            } else {
                $list['one'][$k]['pay_type'] = '银行转账';
            }
        }
        $str = "订单号,加盟商,金额,支付方式,添加时间\n";
        $str = iconv('utf-8','gb2312',$str);
        foreach($list['one'] as $k => $v){
            $order_no = iconv('utf-8','gb2312',$v['order_no']);
            $org_id = iconv('utf-8','gb2312',$v['org_id']);
            $pay_type = iconv('utf-8','gb2312',$v['pay_type']);
            $str .= $order_no . ',' . $org_id . ',' . $v['money'] . ',' . $pay_type . ',' . $v['add_time'] . "\n";
        }
        $filename = '财务管理_'.date('Ymd').'.csv'; //设置文件名
        $this->export_csv($filename,$str); //导出
    } 导出csv
Copy after login

Related recommendations:



The above is the detailed content of How to export Excel in php without plug-ins and enabling configuration? (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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