PHP-Chained operation output Excel (CSV)

Source: Internet
Author: User
Tags php tutorial
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:

But there is a problem, from the code is UTF-8 database out of the output of the Chinese character Excel (CSV) will be garbled, this time you have to note, to get the data before you set the database encoding, for example, my need is utf-8 to output, this time will

$link Mysqli_connect ($host$user$passwd$dbmysqli_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~

The above describes the PHP chain operation output Excel (CSV), including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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