zoukankan      html  css  js  c++  java
  • php如何导出csv文件(代码示例)

    <?php
    $data = [
      ['id'=>1,'name'=>'陈大大','create_time'=>'2019-11-12'],
      ['id'=>2,'name'=>'陈中中','create_time'=>'2019-11-13'],
    ];
    $headerArray = ['序号','用户名','注册时间'];
    $string = implode(",",$headerArray)."
    ";//先做出表头
    foreach ($data as $key => $value) {
      //把需要的字段一一对应起来,如果已经对应好 直接 $outArray = $value;
      $outArray['id'] = $value['id'];
      $outArray['name'] = $value['name'];
      $outArray['create_time'] = $value['create_time'];
      $string .= implode(",",$outArray)."
    "; //用英文逗号分开 
    }
    $filename = date('Ymd').'.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');
    exit(mb_convert_encoding($string, "GBK", "UTF-8"));

    转于:https://www.jianshu.com/p/4683280832f6

  • 相关阅读:
    p3201&bzoj1483 梦幻布丁
    p1341 无序字母对
    p2590&bzoj1036 树的统计
    p1462 通往奥格瑞玛的道路
    p1522 牛的旅行 Cow Tours
    ARC097D Equals
    p2371&bzoj2118 墨墨的等式
    ARC097C K-th Substring
    欧拉函数入门合集(模板)
    主席树
  • 原文地址:https://www.cnblogs.com/zhangzhijian/p/11907241.html
Copyright © 2011-2022 走看看