【php导出excel,大量导出,文本格式】

php导出excel,大量导出,文本格式


php导出excel
特点: 简单,不需要插件,可以导出大量数据,避免内存溢出
缺点: 样式简单
原理: 通过拼接html table标签格式 保存为excel文件
特别说明: td 标签使用 <td style='mso-number-format: \@'> 可以使数字保持为文本格式,用mso-number-format:’@’(这里的@表示文本格式,其他更多的格式,可以参照Excel自定义单元格式的用法)

不说废话直接上代码:

	// 导出excel
    public function exportlist()
    {
    	//前端参数获取
        $id = input('id');
        $userid= input('userid');
        $username= input('username');
		//配置的第二数据源
        $DB_CONFIG_USER= config('DB_CONFIG_USER');
        $dbquery = db('user', $DB_CONFIG_USER);
        if ($id) {
            $where['id'] = array('like', (string)$id . '%');
        }
        if ($username) {
            $where['username'] = array('like', '%' . (string)$username. '%');
        }
        if ($userid) {
            $where['userid'] = $userid;
        }

        $order = 'create_time DESC';
        $list = $dbquery->where($where)
            ->field('id,userid,username,create_time,deal_code')
            ->order($order)->select();
        $fieldname = array(
            'id',
            '用户id',
            '用户名称',
            '创建时间',
            '返回码,0=成功',
        );
        $field = array(
            'id',
            'userid',
            'username',
            'create_time',
            'deal_code',
        );
        $tablename = "导出记录";
        $this->outexcel($list, $fieldname, $field, $tablename);
    }

    //用头部信息输出excel格式文件
    public function outexcel($data, $fieldname, $field, $tablename)
    {
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $tablename . '表(' . date('Ymd-His') . ').xls"');
        header('Cache-Control: max-age=0');
        $count = count($fieldname);
        echo "<body><meta charset='UTF-8'><table border='1'><tr>";
        for ($i = 0; $i < $count; $i++) {
            echo "<th>" . $fieldname[$i] . "</th>";
        }
        echo "</tr>";
        for ($i = 0; $i < count($data); $i++) {
            for ($j = 0; $j < $count; $j++) {
                echo "<td style='mso-number-format: \@'>" . $data[$i]["$field[$j]"] . "</td>";
            }
            echo "</tr>";
        }

        echo "</table></meta charset='UTF-8'></body>";
    }

觉得有用记得给个好评

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值