zoukankan      html  css  js  c++  java
  • PHP 将MySQL数据导出csv

    1.查询数据

        // 假设得到的数据格式如下
        $result = array(
            array(
                "orderid" = "1110111",
                "shopid" = "202302323",
            ),
            array(
                "orderid" = "1110111",
                "shopid" = "202302323",
            )
        );
    

    2.组装数据

        $string = "订单ID,店铺ID
    ";
        foreach($result as $key => $value) {
            $string .= $value['orderid'] . ',' . "	"  . $value['shopid'] . "	
    ";
        }
    

    说明:

    1. 字段值之间用英文 ","隔开;
    2. 遇到数字字符串时结尾加 " ",否则长度超过12位会被转为科学计数法形式;

    3. 改变编码格式

        $string =iconv('utf-8', 'gb2312', $string);
    

    最好将编码转一下,否则execl 下中文乱码

    4. 导出csv

        $filename = date('Y-m-d').'.csv';
        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 $string;
    
  • 相关阅读:
    Python编程四大神兽:迭代器、生成器、闭包和装饰器
    Linux基础
    3.8记录
    3.7记录
    3.6进度记录
    3.5进度
    3.4进度
    3.3进度
    3.2进度记录
    3.1记录
  • 原文地址:https://www.cnblogs.com/chenshuo/p/5367456.html
Copyright © 2011-2022 走看看