Php export CSV abstract class

Source: Internet
Author: User
PHP exports a CSV abstract class. The total number of records and the number of records per batch are calculated and exported cyclically. Avoid insufficient memory. Php exports a CSV abstract class. The total number of records and the number of records per batch are calculated and exported cyclically. Avoid insufficient memory.


ExportCSV. class. php


 0) {$ this-> pagesize = $ pagesize;}/** set the exported file name * @ param String $ filename */public function setExportName ($ filename) {if ($ filename! = '') {$ This-> exportName = $ filename;}/** set the separator * @ param String $ separator */public function setSeparator ($ separator) {if ($ separator! = '') {$ This-> separator = $ separator;}/** set the delimiter * @ param String $ delimiter */public function setDelimiter ($ delimiter) {if ($ delimiter! = '') {$ This-> delimiter = $ delimiter;}/** export csv */public function export () {// obtain the total number of records $ this-> total = $ this-> getExportTotal (); // if (! $ This-> total) {return false;} // calculate the total number of exported batches $ pagecount = $ this-> getPageCount (); // Get the exported column name $ fields = $ this-> getExportFields (); // Set the exported file header $ this-> setHeader (); // cyclically export for ($ I = 0; $ I <$ pagecount; $ I ++) {$ exportData = ''; if ($ I = 0) {// export the column name $ exportData before the first record. = $ this-> formatCSV ($ fields);} // sets the offset value $ offset = $ I * $ this-> pagesize; // Get the data per page $ data = $ this-> getExportData ($ offset, $ this-> pagesize); // Set the data per page Convert data to csv format if ($ data) {foreach ($ data as $ row) {$ exportData. = $ this-> formatCSV ($ row) ;}// export data echo $ exportData ;}/ ** calculate total batches */private function getPageCount () {$ pagecount = (int) ($ this-> total-1)/$ this-> pagesize) + 1; return $ pagecount ;} /** set the exported file header */private function setHeader () {header ('content-type: application/x-msexcel '); $ ua = $ _ SERVER ['http _ USER_AGENT ']; if (preg_match ("/MSIE/", $ ua)) {Header ('content-disposition: attachment; filename = "'. rawurlencode ($ this-> exportName ). '"');} elseif (preg_match ("/Firefox/", $ ua) {header (" content-disposition: attachment; filename * = \ "utf8 ''". $ this-> exportName. '"');} else {header ('content-disposition: attachment; filename = "'. $ this-> exportName. '"');} ob_end_flush (); ob_implicit_flush (true);}/** format data in csv format * @ param Array $ data to be converted to an Array in csv format */pri Vate function formatCSV ($ data = array () {// escape each element of the array $ data = array_map (array ($ this, 'Escape '), $ data ); return $ this-> delimiter. implode ($ this-> delimiter. $ this-> separator. $ this-> delimiter, $ data ). $ this-> delimiter. "\ r \ n";}/** escape String * @ param String $ str * @ return String */private function escape ($ str) {return str_replace ($ this-> delimiter, $ this-> delimiter. $ this-> delimiter, $ str) ;}// class end?>


Demo



 Data);}/** return the exported column name * @ return Array */protected function getExportFields () {$ title = array ('id', 'name ', 'gender'); return $ title ;} /* return records of each batch * @ param int $ offset * @ param int $ number of records obtained by limit * @ return Array */protected function getExportData ($ offset, $ limit) {return array_slice ($ this-> data, $ offset, $ limit) ;}// export $ obj = new myexport (); $ obj-> setPageSize (1 ); $ obj-> setExportName('myexport.csv '); $ Obj-> setSeparator (', '); $ obj-> setDelimiter (' "'); $ obj-> export ();?>


The above is the content of the CSV abstract class exported by php. For more information, see PHP Chinese website (www.php1.cn )!

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.