HP outputs data in excel (csv) format through chained operations-PHP source code

Source: Internet
Author: User
CSV is a common and relatively simple file format that is widely used by users, businesses, and science. The most widely used is the transfer of table data between programs, and these programs themselves operate in incompatible formats. Now let's learn how to output the excel (csv) format using PHP chained operations. CSV is a common and relatively simple file format that is widely used by users, businesses, and science. The most widely used is the transfer of table data between programs, and these programs themselves operate in incompatible formats. Now let's learn how to output the excel (csv) format using PHP chained operations.

Script ec (2); script

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.

The Code is as follows:
Class Array2csv {
/*
* @ Var string $ ext Extension
*/
Private $ ext = 'csv ';

/**
* @ Desc Constructor
* @ Param string $ filename name of the file to be 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 "csv file r cannot be exported in cli mode ";
$ 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 row 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 multiple lines of excel content
* @ Param array $ bodyArr multiple rows 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 (
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 ', 'running man6', 'running userman ')
);
$ Test-> title (array ('test', 'ha ', '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

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.