DDR爱好者之家 Design By 杰米

最近做了一个项目需要把订单的信息显示出来,并且能够把相关信息放到一个.csv 文件中,下载到浏览器。虽然说csv是一种比较简单的excel表格形式,生成只要按指定格式然后生成.csv文件就可以,但是在使用中也会遇到很多问题,下面给大家分享下PHP下载csv文件及问题总结

首先大家先看个例子,生成csv文件并下载

//要生成csv文件的数组
$csvArr=array();
$csvArr[]=array('用户编号1','上班日期1','签到时间1','签退时间1');
$csvArr[]=array('用户编号2','上班日期2','签到时间2','签退时间2')
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
$head=array('用户编号','上班日期','签到时间','签退时间');
echo array2csv($csvArr,$head);
unset($csvArr);
die();
function array2csv(array &$array,$head)
{
  if (count($array) == 0) {
   return null;
  }
  ob_start();
  $df = fopen("php://output", 'w');
  if(!$head){
    $head=array_keys(reset($array));
  }
  fputcsv($df,$head);
  foreach ($array as $row) {
   fputcsv($df, $row);
  }
  fclose($df);
  return ob_get_clean();
}
function download_send_headers($filename) {
  // disable caching
  $now = gmdate("D, d M Y H:i:s");
  header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
  header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
  header("Last-Modified: {$now} GMT");
  // force download 
  header("Content-Type: application/force-download");
  header("Content-Type: application/octet-stream");
  header("Content-Type: application/download");
  // disposition / encoding on response body
  header("Content-Disposition: attachment;filename={$filename}");
  header("Content-Transfer-Encoding: binary");
}

php array生成csv文件

<"example";
  header("Content-type: text/csv");
  header("Content-Disposition: attachment; filename={$filename}.csv");
  header("Pragma: no-cache");
  header("Expires: 0");
outputCSV($data);
function outputCSV($data) {
    $outputBuffer = fopen("php://output", 'w');
    foreach($data as $val) {
    foreach ($val as $key => $val2) {
     $val[$key] = iconv('utf-8', 'gbk', $val2);
// CSV的Excel支持GBK编码,一定要转换,否则乱码
     }
      fputcsv($outputBuffer, $val);
    }
    fclose($outputBuffer);
  }
"htmlcode">
function __fgetcsv(& $handle, $length = null, $d = ',', $e = '"') {
   $d = preg_quote($d);
   $e = preg_quote($e);
   $_line = "";
   $eof=false;
   while ($eof != true) {
     $_line .= (empty ($length) "htmlcode">
<"D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
$items_data=array(
'0'=>array('title'=>'test test test1'),
'1'=>array('title'=>'test test test2'),
'2'=>array('title'=>'test test test3')
)
print(chr(0xEF).chr(0xBB).chr(0xBF));
//设置utf-8 + bom ,处理汉字显示的乱码
echo array2csv($items_data);
function array2csv(array &$array)
{
  if (count($array) == 0) {
   return null;
  }
  ob_start();
  $df = fopen("php://output", 'w');
  fputcsv($df, array_keys(reset($array)));
  foreach ($array as $row) {
   fputcsv($df, $row);
  }
  fclose($df);
  return ob_get_clean();
}
"\t".
php 程序可以这样判断,注意一定是"\t",不是'\t'.

复制代码 代码如下:
is_numeric($val)"\t":$val;

以上代码内容就是本文实现PHP下载生成的csv文件及问题总结,希望大家喜欢。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米