php导出csv数据在浏览器中输出提供下载或保存到文件的示例_PHP教程

WBOY
Release: 2016-07-13 10:31:52
Original
772 people have browsed it

1.在浏览器输出提供下载

复制代码 代码如下:

/**
 * 导出数据到CSV文件
 * @param array $data  数据
 * @param array $title_arr 标题
 * @param string $file_name CSV文件名
 */
function export_csv(&$data, $title_arr, $file_name = '') {
    ini_set("max_execution_time", "3600");

    $csv_data = '';

    /** 标题 */
    $nums = count($title_arr);
    for ($i = 0; $i         $csv_data .= '"' . $title_arr[$i] . '",';
    }

    if ($nums > 0) {
     $csv_data .= '"' . $title_arr[$nums - 1] . "\"\r\n";
    }

    foreach ($data as $k => $row) {
        for ($i = 0; $i             $row[$i] = str_replace("\"", "\"\"", $row[$i]);
            $csv_data .= '"' . $row[$i] . '",';
        }
        $csv_data .= '"' . $row[$nums - 1] . "\"\r\n";
        unset($data[$k]);
    }

    $csv_data = mb_convert_encoding($csv_data, "cp936", "UTF-8");

    $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;

    if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")) { // 解决IE浏览器输出中文名乱码的bug
     $file_name = urlencode($file_name);
     $file_name = str_replace('+', '%20', $file_name);
    }

    $file_name = $file_name . '.csv';
    header("Content-type:text/csv;");
    header("Content-Disposition:attachment;filename=" . $file_name);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo $csv_data;
}

2.保存到文件

复制代码 代码如下:

function export_csv($data, $title_arr, $file_name = '') {


    $csv_data = '';


    /** 标题 */
    $nums = count($title_arr);
    for ($i = 0; $i         $csv_data .= '"' . $title_arr[$i] . '",';
    }


    if ($nums > 0) {
    $csv_data .= '"' . $title_arr[$nums - 1] . "\"\r\n";
    }


    foreach ($data as $k => $row) {
        for ($i = 0; $i             $row[$i] = str_replace("\"", "\"\"", $row[$i]);
            $csv_data .= '"' . $row[$i] . '",';
        }
        $csv_data .= '"' . $row[$nums - 1] . "\"\r\n";
        unset($data[$k]);
    }


    $file_name = empty($file_name) ? date('Y-m-d-H-i-s', time()) : $file_name;
    file_put_contents($file_name, $csv_data) ;
}

调用示例(保存到文件):

复制代码 代码如下:

$file_name="/var/www/tmp/test.csv" ;

$header = array(
                    '0' => '参数ID',
                    '1' => '参数名称',
                    '2' => '统计次数',
                    '3' => '统计次数百分比',
                    '4' => '唯一用户数',
                    '5' => '唯一用户数百分比',
                    '6' => '人均次数'
            );
            $csvList = array(array("111", "title", "12", "100%", "23", "50%", "4")) ;
            export_csv($csvList, $header, $file_name) ;

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/760288.htmlTechArticle1.在浏览器输出提供下载 复制代码 代码如下: /** * 导出数据到CSV文件 * @paramarray$data数据 * @paramarray$title_arr标题 * @paramstring$file_nameCSV文件名...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!