PHP chained operation output excel (csv)

Source: Internet
Author: User
: This article mainly introduces the PHP chain operation output excel (csv). If you are interested in the PHP Tutorial, please refer to it. In our work, we often encounter product operations that allow you to export some simple and standardized data. at this time, it would be much easier to use a simple method. The following is a simple excel (csv) Method class that I use for chained operations. When it comes to chained operations, which may be frequently used in jquery, do you also feel that chained operations are quite helpful? we also implement chained operations in this class.

In fact, the chain operation is very simple, that is, the previous class method returns an object of this class ($ this), provided to the next method call.

 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 line of the title to be output * @ param object Array2csv object itself */public function title ($ title) {$ title = implode (",", $ title); echo $ title. "\ n"; return $ this;}/*** @ desc print a row of excel content * @ param array $ body content to be output * @ param object Array2csv object itself */public Function body ($ body) {if (! Is_array ($ body) | empty ($ body) {return false;} $ body = implode (",", $ body); echo $ body. "\ n"; return $ this ;} /*** @ desc print multi-row excel content * @ param array $ bodyArr multi-row content to be output * @ param object Array2csv object itself */public function multiBody ($ 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 = new Array2csv ('test'); $ arr = array ('luluyrt @ 163.com ', 'runningman1', 'runninguserman'), array ('luluyrt @ 163.com ', 'runningman2', 'runninguserman '), array ('luluyrt @ 163.com ', 'Running man3', 'Running userman'), array ('luluyrt @ 163.com ', 'Running man4 ', 'runninguserman'), array ('luluyrt @ 163.com ', 'runningman5', 'runninguserman'), array ('luluyrt @ 163.com ', 'runningman6', 'runninguserman'); $ test-> title (array ('test', 'hha ', 'Haha ')) -> body (array ('2017, sadkl ', 'sdsas', 'sdvsvdd sub')-> multiBody ($ arr );

Shows the output csv file:

But there is a problem here. the output of Chinese characters obtained from the database with UTF-8 encoding will be garbled in excel (csv). Please note that, before getting data, you need to set the database encoding. for example, if you need UTF-8 for output, you need

$link = mysqli_connect($host, $user, $passwd, $db); mysqli_query($link, "set names utf8");

Only after encoding conversion can the information be displayed normally. the following shows the information about my database and the effect before and after encoding:

Database information

Comparison before and after Database encoding

Send Me ~

The above introduces the PHP chain operation output excel (csv), including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.