求数据保存到csv文件的方法。
1 回答
牵猪的仓鼠
TA贡献3条经验 获得超0个赞
输出csv 跟框架无关,我给你一个代码
// 数据导出组件 final class SimpleDataExport { static function generateResponseHeader($filename,$charset='UTF-8',$mimeType = 'application/octet-stream'){ header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: {$mimeType}; charset={$charset}"); header("Content-Transfer-Encoding: binary"); header(self::cDispositionHeader($_SERVER["HTTP_USER_AGENT"],$filename,'attachment',$charset)); } static function cDispositionHeader($httpUserAgent,$filename,$contentDisposition='attachment',$charset = 'UTF-8'){ // 文件名乱码问题 if (preg_match("/MSIE/", $httpUserAgent)) { $filename = urlencode($filename); $filename = str_replace("+", "%20", $filename);// 替换空格 $attachmentHeader = "Content-Disposition: {$contentDisposition}; filename=\"{$filename}\"; charset={$charset}"; } else if (preg_match("/Firefox/", $httpUserAgent)) { $attachmentHeader = 'Content-Disposition: '.$contentDisposition.'; filename*="utf8\'\'' . $filename. '"' ; } else { $attachmentHeader = "Content-Disposition: '.$contentDisposition.'; filename=\"{$filename}\"; charset={$charset}"; } return $attachmentHeader; } } /** * 简易 csv 数据导出类 * */ class SimpleCsvExport { static function generateXlsHeader($filename){ SimpleDataExport::generateResponseHeader($filename,'utf-8','text/csv'); echo(chr(0xEF).chr(0xBB).chr(0xBF)); } static function writeRow(array $row){ echo implode(',',$row) . PHP_EOL; } }
- 1 回答
- 0 关注
- 1714 浏览
添加回答
举报
0/150
提交
取消