Home >Backend Development >PHP Tutorial >PHP generates and downloads csv files and solves problems

PHP generates and downloads csv files and solves problems

墨辰丷
墨辰丷Original
2018-06-07 17:14:264928browse

本篇文章主要介绍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文件

<?php
$data = array(
    array( &#39;row_1_col_1&#39;, &#39;row_1_col_2&#39;, &#39;row_1_col_3&#39; ),
    array( &#39;row_2_col_1&#39;, &#39;row_2_col_2&#39;, &#39;row_2_col_3&#39; ),
    array( &#39;row_3_col_1&#39;, &#39;row_3_col_2&#39;, &#39;row_3_col_3&#39; ),
  );
$filename = "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", &#39;w&#39;);
    foreach($data as $val) {
    foreach ($val as $key => $val2) {
     $val[$key] = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $val2);
// CSV的Excel支持GBK编码,一定要转换,否则乱码
     }
      fputcsv($outputBuffer, $val);
    }
    fclose($outputBuffer);
  }
?>

解决 fgetcsv函数在php5.2.8 中的bug

环境linux

问题解析出来的数据不完整,有为空的字段

网上查了下说是在php5.2.8 中存在bug

解决办法是使用自定义函数

function __fgetcsv(& $handle, $length = null, $d = &#39;,&#39;, $e = &#39;"&#39;) {
   $d = preg_quote($d);
   $e = preg_quote($e);
   $_line = "";
   $eof=false;
   while ($eof != true) {
     $_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
     $itemcnt = preg_match_all(&#39;/&#39; . $e . &#39;/&#39;, $_line, $dummy);
     if ($itemcnt % 2 == 0)
       $eof = true;
   }
   $_csv_line = preg_replace(&#39;/(?: |[ ])?$/&#39;, $d, trim($_line));
   $_csv_pattern = &#39;/(&#39; . $e . &#39;[^&#39; . $e . &#39;]*(?:&#39; . $e . $e . &#39;[^&#39; . $e . &#39;]*)*&#39; . $e . &#39;|[^&#39; . $d . &#39;]*)&#39; . $d . &#39;/&#39;;
   preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
   $_csv_data = $_csv_matches[1];
   for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
     $_csv_data[$_csv_i] = preg_replace(&#39;/^&#39; . $e . &#39;(.*)&#39; . $e . &#39;$/s&#39;, &#39;$1&#39; , $_csv_data[$_csv_i]);
     $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
   }
   return empty ($_line) ? false : $_csv_data;
}

excel无法正确读取长度超过32K的CSV域问题
php 导出csv文件用excel打开后,产品表述字段分两行显示。
查看了下这个字段发现这个字段超过32K的字符,excel会把字符串打断成两行,如果小于32K,显示正常。
这是EXCEL的限制,目前还没有找到解决办法。
excel一个单元格最多是32767个字符。
解决PHP生成UTF-8编码的CSV文件用Excel打开乱码的问题
PHP生成UTF-8编码的CSV文件用Excel打开中文显示乱码,是由于输出的CSV文件中没有BOM。

<?php
$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");
$items_data=array(
&#39;0&#39;=>array(&#39;title&#39;=>&#39;test test test1&#39;),
&#39;1&#39;=>array(&#39;title&#39;=>&#39;test test test2&#39;),
&#39;2&#39;=>array(&#39;title&#39;=>&#39;test test test3&#39;)
)
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", &#39;w&#39;);
  fputcsv($df, array_keys(reset($array)));
  foreach ($array as $row) {
   fputcsv($df, $row);
  }
  fclose($df);
  return ob_get_clean();
}
?>

导出csv文件数字会自动变科学计数法的解决方法
用程序导出的csv文件,当字段中有比较长的数字字段存在时,在用excel软甲查看csv文件时就会变成科学技术法的表现形式。
其实这个问题跟用什么语言导出csv文件没有关系。Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法;如果数字大于15位,它不仅用于科学技术费表示,还会只保留高15位,其他位都变0。
解决这个问题
只要把数字字段后面加上显示上看不见的字符即可,字符串结尾加上制表符"\t".
php 程序可以这样判断,注意一定是"\t",不是'\t'.

代码如下:

is_numeric($val)?$val."\t":$val;

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php实现保存周期为一天的购物车类详解

php的ob缓存机制实现页面静态化的方法详解

PHP实现正则匹配操作的方法

The above is the detailed content of PHP generates and downloads csv files and solves problems. For more information, please follow other related articles on the PHP Chinese website!

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