PHP Exports the CSV abstract class, calculates the total batches according to the total number of records and the number of records per batch, and loops the export. Avoid running out of memory problems.
ExportCSV.class.php
<?php/** PHP Export CSV abstract class, calculates the total batches based on the total number of records and the number of records per batch, and loops out. * date:2014-05-16* author:fdipzone* ver:1.0** func:* Public setpagesize set number of records exported per batch * public SE Texportname SET Export filename * Public setseparator Set delimiter * Public Setdelimiter set delimiter * Public export execution Export * Private Getpagecount calculation Export Total batch * Private SetHeader set export file header* private formatcsv format data in CSV format * Private Escape Escape String * Abstract getexporttotal Gets the total number of records, abstract method, need to inherit class implementation * Abstract Getexportfields Get the exported column name, abstract method, Inheriting class implementations * Abstract Getexportdata gets each page of records, abstraction method, inheriting class implementation */abstract class Exportcsv{//Class Start//define subclass the method that must be implemented /** get total number of records * @return int */abstract protected function getexporttotal (); /** gets the exported column name * @return Array */abstract protected function getexportfields (); /** Get data per batch * @param int $offset offset * @param int $limit number of records obtained * @return Array */abstract protected F Unction Getexportdata ($oFfset, $limit); Define class properties Protected $total = 0; Total Record count protected $pagesize = 500; Number of records exported per batch protected $exportName = ' export.csv '; The exported file name protected $separator = ', '; Set delimiter protected $delimiter = ' "; Set delimiter/** sets the number of record entries per export * @param int $pagesize Number of record entries per export */Public Function setpagesize ($pagesize =0) { if (Is_numeric ($pagesize) && $pagesize >0) {$this->pagesize = $pagesize; }}/** set the exported file name * @param String $filename The exported file name */Public Function Setexportname ($filename) {if ($f Ilename!= ") {$this->exportname = $filename; }}/** Set delimiter * @param String $separator delimiter */Public Function Setseparator ($separator) {if ($separa Tor!= ") {$this->separator = $separator; }}/** Set delimiter * @param String $delimiter delimiter */Public Function Setdelimiter ($delimiter) {if ($delimi Ter!= "){$this->delimiter = $delimiter; }}/** exports CSV */Public Function export () {//Get total record count $this->total = $this->getexporttotal (); No record if (! $this->total) {return false; }//Calculate export total Batch $pagecount = $this->getpagecount (); Gets the exported column name $fields = $this->getexportfields (); Set export file header $this->setheader (); Loop export for ($i =0; $i < $pagecount; $i + +) {$exportData = '; The IF ($i ==0) {////first record is exported before the column name $exportData. = $this->formatcsv ($fields); }//Set offset value $offset = $i * $this->pagesize; Get data per page $data = $this->getexportdata ($offset, $this->pagesize); Convert each page of data to CSV format if ($data) {foreach ($data as $row) {$exportData. = $this Formatcsv ($row); }}//Export Data ECho $exportData; }}/** calculates the total batch */Private function Getpagecount () {$pagecount = (int) (($this->total-1)/$this->pagesiz e) +1; return $pagecount; }/** set the export 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->expor tname). ' "');} 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); }/** formatted data in CSV format * @param array $data to convert to CSV format */Private Function Formatcsv ($data =array ()) {//logarithm Each element of the group is escaped $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_REPL Ace ($this->delimiter, $this->delimiter $this->delimiter, $STR); }}//Class end?>
Demo
<?php//exportcsv abstract Classrequire "ExportCSV.class.php";//define inheriting class class Myexport extends exportcsv{//data to be exported, actual The situation will read from DB protected $data = Array (Array (' 1 ', ' Proud snow star Maple ', ' male '), Array (' 2 ', ' Proud snow star Maple ', ' ', ' Male '), Array (' 3 ', ' Proud snow star Maple "," ', ' Male '), Array (' 4 ', "Proud snow star maple \" \ "\ r \ n", ' Male '), Array (' 5 ', ' Proud snow star Maple,, ', ' Male '), Array (' 6 ', ' Proud snow star Maple ', ' male '), Array (' 7 ', ' Proud snow star Maple ', ' male '), array (' 8 ', ' Proud snow star Maple ', ' male '), Array (' 9 ', ' Proud snow star Maple ', ' male '), Array (' 10 ', ' Proud snow star Maple ', ' Male ')); /* Returns the total number of exported records * @return int */protected function getexporttotal () {return count ($this->data); /** returns the exported column name * @return Array */protected function Getexportfields () {$title = array (' id ', ' name ', ' Gend Er '); return $title; */* returns records per batch * @param int $offset offset * @param int $limit number of records obtained * @return Array * * Protected funct Ion 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 PHP export CSV abstract class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!