美文网首页
PHP 导出excel

PHP 导出excel

作者: aoshi | 来源:发表于2021-01-25 16:30 被阅读0次

    注意

    1.导出文件乱码 需要注意数据库数据字段字符编码和文件字符编码是否一致【不一致需要使用iconv函数变化字符串】
    

    浏览器导出实例

                $fileName = date('Y-m-d').'_del.csv';      //文件名
                set_time_limit(0);
                ini_set('memory_limit', '1024M');
    
                header('Content-Type: application/vnd.ms-excel');    //输出文件类型
                header('Content-Disposition: attachment;filename="'.$fileName.'"');    //输出文件名
                header('Cache-Control: max-age=0');    //强制每次请求直接发送给源服务器,而不经过本地缓存版本的校验
                // 设置标题
                $title = ["t1", "t2","t3", "t4","t5","6者", "t7","t8","t9","t10"];
                $fp = fopen('php://output', 'a');
                fputcsv($fp, $title);
    
                foreach($list as $k=>$v){
                    if($k/300 == 0){
                        ob_flush();
                        flush();
                    }
    
                    $str_arr = array();
                    $str_arr[] = $v['1'];
                    $str_arr[] = $v['2'];
                    $str_arr[] = $v['3'];
                    $str_arr[] = $v['4'];
    
                    $str_arr[] = $sourceList[$v['5']];
                    $str_arr[] = $zmtUMap[$v['6']];
                    $str_arr[] = $v['7'];
                    $str_arr[] = $v['8'];
                    $str_arr[] = $feedChgReason[$v['9']];
                    $str_arr[] = date('Y-m-d H:i',$v['10']);
                    fputcsv($fp, $str_arr);
                }
                fclose($fp);
    

    服务器脚本导出实例

                $fileName = date('Y-m-d').'_del.csv';      //文件名
                $baseName = '/exportDir/';          //绝对路径写法 根据自身情况选择
                //$baseName = 'exportDir/';          //相对路径写法 根据自身情况选择
                $filePath = $baseName  . $fileName;
                set_time_limit(0);
                ini_set('memory_limit', '1024M');
    
                // 设置标题
                $title = ["t1", "t2","t3", "t4","t5","6者", "t7","t8","t9","t10"];
                $fp = fopen($filePath, 'a');
                fputcsv($fp, $title);
    
                foreach($list as $k=>$v){
                    if($k/300 == 0){
                        ob_flush();
                        flush();
                    }
    
                    $str_arr = array();
                    $str_arr[] = $v['1'];
                    $str_arr[] = $v['2'];
                    $str_arr[] = $v['3'];
                    $str_arr[] = $v['4'];
    
                    $str_arr[] = $sourceList[$v['5']];
                    $str_arr[] = $zmtUMap[$v['6']];
                    $str_arr[] = $v['7'];
                    $str_arr[] = $v['8'];
                    $str_arr[] = $feedChgReason[$v['9']];
                    $str_arr[] = date('Y-m-d H:i',$v['10']);
                    fputcsv($fp, $str_arr);
                }
                fclose($fp);
    

    相关文章

      网友评论

          本文标题:PHP 导出excel

          本文链接:https://www.haomeiwen.com/subject/knkizktx.html