Created
September 1, 2012 16:43
-
-
Save outman/3579918 to your computer and use it in GitHub Desktop.
Export CSV Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Base ON Zhen.liu's SourceCode | |
* Add isset function for user agent | |
* Change line end | |
*/ | |
class CSV { | |
private static $fp; | |
public static function init($filename='') { | |
if (!$filename) $filename = date('Ymd'); | |
$user_agent = isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:''; | |
if(preg_match("/MSIE/", $user_agent)) { | |
$filename = urlencode($filename); | |
} | |
//setting csv header | |
header("Content-type:text/csv"); | |
header("Content-Disposition:attachment;filename=".$filename.".csv"); | |
header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); | |
header('Expires:0'); | |
header('Pragma:public'); | |
self::$fp = fopen('php://output', 'w'); | |
//add BOM header(unicode) | |
fwrite(self::$fp,"\xFF\xFE", 2); | |
} | |
public static function export_header($header=array()) { | |
//add header line | |
fwrite(self::$fp, self::encoding_convert(implode("\t",$header))); | |
fwrite(self::$fp, self::encoding_convert(PHP_EOL)); | |
} | |
public static function export_data($data=array()) { | |
fwrite(self::$fp, self::encoding_convert(implode("\t",$data))); | |
fwrite(self::$fp, self::encoding_convert(PHP_EOL)); | |
} | |
public static function del() { | |
fclose(self::$fp); | |
} | |
private function encoding_convert($str=''){ | |
return iconv('UTF-8','UCS-2LE',$str); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
*********************************************************************** | |
*@description csv_helper是导出csv的辅助类,目前支持从UTF8转换到UCS-2LE | |
*@author zhen.liu | |
*@date 2011-09-21 | |
*@version 1.0 | |
*********************************************************************** | |
*/ | |
class CSV { | |
private static $fp; | |
public static function init($filename='') { | |
if (!$filename) $filename = date('Ymd'); | |
$user_agent = $_SERVER['HTTP_USER_AGENT']; | |
if(preg_match("/MSIE/", $user_agent)) { | |
$filename = urlencode($filename); | |
} | |
//设置 csv header | |
header("Content-type:text/csv"); | |
header("Content-Disposition:attachment;filename=".$filename.".csv"); | |
header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); | |
header('Expires:0'); | |
header('Pragma:public'); | |
//打开标准输出 | |
self::$fp = fopen('php://output', 'w'); | |
//add BOM header(unicode) | |
fwrite(self::$fp,"\xFF\xFE", 2); | |
} | |
public static function export_header($header=array()) { | |
//add header line | |
fwrite(self::$fp, self::encoding_convert(implode("\t",$header))); | |
} | |
public static function export_data($data=array()) { | |
fwrite(self::$fp, self::encoding_convert("\r\n")); | |
fwrite(self::$fp, self::encoding_convert(implode("\t",$data))); | |
} | |
public static function del() { | |
fclose(self::$fp); | |
} | |
private function encoding_convert($str=''){ | |
return iconv('UTF-8','UCS-2LE',$str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment