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' );
        }

  • 相关阅读:
    hudson搭建经验总结(二)
    CodePen最佳实例分享
    hudson搭建经验总结
    资料文件夹管理系统
    ueditor+word图片上传
    asp.net上传大文件
    UEditor粘贴word
    大文件上传组件
    文件资源管理系统
    ueditor+复制word图片粘贴上传
  • 原文地址:https://www.cnblogs.com/chunyin/p/2678637.html
Copyright © 2011-2022 走看看