zoukankan      html  css  js  c++  java
  • php 将数据下载为csv文件

        /**
         * 下载 CSV 数据为CSV文件
         */
        public function downloadCsvData( $csv_data = array())
        {
            /****************************************************************************************************************************
             * 新建csv数据
            /****************************************************************************************************************************/
            $csv_string = null;
            $csv_row    = array();
            foreach( $csv_data as $key => $csv_item )
            {
                if( $key === 0 )
                {
                    $csv_row[]    = implode( "\t" , $csv_item );
                    continue;
                }
                $current    = array();
                foreach( $csv_item AS $item )
                {

              /****************************************************************************************************************************
               *很关键。 默认csv文件字符串需要 ‘ " ’ 环绕,否则导入导出操作时可能发生异常。
              /****************************************************************************************************************************/
                    $current[] = is_numeric( $item ) ? $item : '"' . str_replace( '"', '""', $item ) . '"';
                }
                $csv_row[]    = implode( "\t" , $current );
            }
            $csv_string = implode( "\r\n", $csv_row );
            /****************************************************************************************************************************
             * 输出
            /****************************************************************************************************************************/
             header("Content-type:text/csv");
             header("Content-Type: application/force-download");
            header("Content-Disposition: attachment; filename=data_package.".date('Y-m-d').".csv");
            header('Expires:0');
            header('Pragma:public');
            echo "\xFF\xFE".mb_convert_encoding( $csv_string, 'UCS-2LE', 'UTF-8' );
        }

  • 相关阅读:
    arguments.callee
    vue的生命周期
    Vue中的v-cloak用法
    控制input只能输入数字和两位小数
    css3新单位vw、vh的使用详解
    关于图片的Base64编码
    Logic and Fault simulation
    scan design flow(二)
    scan design flow(一)
    异构计算
  • 原文地址:https://www.cnblogs.com/chunyin/p/2678637.html
Copyright © 2011-2022 走看看