Home  >  Article  >  Backend Development  >  PHP exports csv data and outputs it in the browser. Provides examples of downloading or saving to files_PHP tutorial

PHP exports csv data and outputs it in the browser. Provides examples of downloading or saving to files_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:52853browse

1. Provide download in browser output

Copy code The code is as follows:

/**
* Export data to CSV file
* @param array $data Data
* @param array $title_arr Title
* @param string $file_name CSV file name
*/
function export_csv(&$data, $title_arr, $file_name = '') {
ini_set("max_execution_time", "3600");

$csv_data = '';

/**title*/
$nums = count($title_arr);
for ($i = 0; $i < $nums - 1; ++$i) {
        $csv_data .= '"' . $title_arr[$i] . '",';
}

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

foreach ($data as $k => $row) {
for ($i = 0; $i < $nums - 1; ++$i) {
$row[$i ] = str_replace(""", """", $row[$i]);
             $csv_data .= '"' . $row[$i] .       ,'; >       $csv_data .= '"' . $row[$nums - 1] . ""rn";
        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")) { // Solve the bug of IE browser outputting garbled Chinese names

$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. Save to file

Copy code

The code is as follows:function export_csv($data, $title_arr, $file_name = '') {
$csv_data = '';

/**title*/
$nums = count($title_arr);

for ($i = 0; $i < $nums - 1; ++$i) {

$csv_data .= '"' . $title_arr[$i] . '",';
}


if ($nums > 0) {
$csv_data .= '"' . $title_arr[$nums - 1] . ""rn";

}



foreach ($data as $k => $row) {
for ($i = 0; $i < $nums - 1; ++$i) {

$row [$i] = str_replace(""", """", $row[$i]);

               $csv_data .= '"' . $row[$i] . '",';
}
          $csv_data .= '"' . $row[$nums - 1] . ""rn";
                                                                                                                                                                                                                                             $csv_data.

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



Call example
(save to file):

Copy code The code is as follows:


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

> times',

                                                                                                                                                                                                                                                                                                 > ' 6' => 'Number of times per capita'
          ); "4")) ;
export_csv($csvList, $header, $file_name) ;






http://www.bkjia.com/PHPjc/760288.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/760288.html

1. Provide download copy code in the browser output. The code is as follows: /** * Export data to CSV file* @ paramarray$data data* @paramarray$title_arrtitle* @paramstring$file_nameCSV file name...
Statement:
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