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: |
Copy code |
<? Php 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 ); |
The output csv is shown in the following figure:
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