PHP chained operation output Excel (CSV), chained csv_php tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.