PHP chained operation output Excel (CSV), chained CSV
Work often encounter product operations to export some simple comparative specifications of the data, at this time if there is a simple way to use is much simpler. Here is one of my output simple Excel (CSV) method classes, with the use of chained operations. When it comes to chained operations, it can often be used in jquery, is it also felt that the chain operation is very cool, we also implement the chain operation in this class.
In fact, the chain operation is very simple, that is, the previous class method to return the object of the Class ($this), provided to the next method call.
Phpclassarray2csv{/** @var string $ext extension*/ Private $ext= ' csv '; /** * @desc Construction method * @param string $filename file name to Output * @param string $ext extension*/ Public function__construct ($filename,$ext=NULL){ Ob_start(); Header("Content-type:text/html;charset=utf-8"); Header("Content-type:application/x-csv"); if(Php_sapi = = ' CLI ')Echo"Cannot export CSV file in CLI mode \ r"; $this->ext =$ext===NULL?$this->ext:$ext; Header("Content-disposition:attachment;filename=".$filename.".".$this-ext); Ob_flush(); return $this; } /** * @desc Print the Excel title * @param array $title the header row to output * @param the object Array2csv objects themselves*/ Public functionTitle$title){ $title=implode(",",$title); Echo $title." \ n "; return $this; } /** * @desc print a single line of Excel content * @param array $body what to Output * @param object Array2csv objects themselves*/ Public functionBody$body){ if(!Is_array($body) ||Empty($body)) { return false; } $body=implode(",",$body); Echo $body." \ n "; return $this; } /** * @desc print multiple lines of Excel content * @param array $BODYARR The multiline content to be output * @param object Array2csv objects themselves*/ Public functionMultibody ($BODYARR){ if(!Is_array($BODYARR) ||Empty($BODYARR))return false; foreach($BODYARR as $key=$value) { if(Is_array($value)){ $value=implode(",",$value); Echo $value." \ n "; } } return $this; }}$test=NewArray2csv (' Test ');$arr=Array( Array(' luluyrt@163.com ', ' Running Man1 ', ' Running ' Userman '),Array(' luluyrt@163.com ', ' Running Man2 ', ' Running ' Userman '),Array(' luluyrt@163.com ', ' Running Man3 ', ' Running ' Userman '),Array(' luluyrt@163.com ', ' Running Man4 ', ' Running ' Userman '),Array(' luluyrt@163.com ', ' Running Man5 ', ' Running ' Userman '),Array(' luluyrt@163.com ', ' The Man6 of the run ', ' the Userman of the Run '));$test->title (Array(' Test ', ' hehe ', ' haha ')) ->body (Array(' 100,sadkl ', ' Sdsas ', ' Sdvsvdd min ')) ->multibody ($arr);
The output of the CSV is as follows:
$link = mysqli_connect($host, $user, $passwd, $db ); Mysqli_query ($link, "Set names UTF8");
The encoding converter can be displayed normally, here is my database information and the effect before and after encoding:
Database Information
Set the comparison before and after database encoding
Send me~
http://www.bkjia.com/PHPjc/924354.html www.bkjia.com true http://www.bkjia.com/PHPjc/924354.html techarticle PHP chain operation output Excel (CSV), chained CSV work often encountered product operations to export some simple comparative specifications of the data, if there is a simple way to use ...