This article describes the PHP export CSV abstract class and its application, share for everyone to reference. The specific analysis is as follows:
The PHP export CSV abstract class, based on the total number of records and the number of records per batch, calculate the total batch, circular export. Avoid the problem of low memory.
The ExportCSV.class.php class files are as follows:
<?php/** PHP Export CSV abstract class, calculates the total batch and loops out according to the total number of records and the number of records per batch. * date:2014-05-16 * Author:fdipzone * ver:1.0 * * * Func: * Public SetPageSize set the number of record bars exported per batch * public Sete Xportname set the exported filename * Public setseparator Set delimiter * Public Setdelimiter set delimiter * Public export perform export * privat e getpagecount calculation Export Total lot * Private SetHeader Set Export file header * Private formatcsv Format data as CSV * Private escape Escape String * Abstract getexporttotal Gets the total number of records, abstract method, need to inherit the class implementation * Abstract Getexportfields Get the exported column name, abstract method, need to inherit class to implement * abstract gets
ExportData fetch every page of records, abstract methods, need to inherit class implementation */abstract class exportcsv{//Class Start//define subclass must implement method/** get total record number
* @return int */abstract protected function getexporttotal ();
/** gets the exported column name * @return Array */abstract protected function getexportfields (); /** gets the number of record bars per batch data * @param int $offset offset * @param int $limit * @return Array/Abstract protected Func tion Getexportdata ($offset, $lIMIT); Define class attribute protected $total = 0; Total record number protected $pagesize = 500; Number of records exported per lot protected $exportName = ' export.csv '; The exported filename protected $separator = ', '; Set separator protected $delimiter = ' "';
Set the delimiter/** set the number of record bars per export * @param int $pagesize The number of record bars per export */Public Function setpagesize ($pagesize =0) {
if (Is_numeric ($pagesize) && $pagesize >0) {$this->pagesize = $pagesize; /** set the exported filename * @param String $filename the exported filename/Public function setexportname ($filename) {if ($f
Ilename!= ') {$this->exportname = $filename; /** Set delimiter * @param String $separator Separator */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 number $this->total = $this->getexporttotal ();
No record if (! $this->total) {return false;
//Calculate the total batch $pagecount = $this->getpagecount ();
Gets the exported column name $fields = $this->getexportfields ();
Set the export file header $this->setheader ();
Loop export for ($i =0; $i < $pagecount; $i + +) {$exportData = ';
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);
Converts each page of data to CSV format if ($data) {foreach ($data as $row) {$exportData. = $this->formatcsv ($row);
}///Export data echo $exportData; /** Calculate Total lot */Private function Getpagecount () {$pagecount = (int) ($this->total-1)/$thiS->pagesize) +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-> 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); /** formatted in CSV format data * @param array $data to be converted to CSV-formatted arrays/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_replace $this->delimiter, $this->delim
Iter. $this->delimiter, $STR); }//Class end?>
The
Demo sample program is as follows:
<?php//Exportcsv abstract class require "ExportCSV.class.php"; Defines the inherited class class Myexport extends exportcsv{//data to be exported, which is actually read from DB protected $data = Array (' 1 ', ' Snow Star ', ' Male '), Array (' 2 ', ' Snow star Maple ', "', ' Male '), Array (' 3 ', ' Snow star Maple ', ', ', ' Male '), Array (' 4 '," Snow star maple \ "\ \ \ \ \ \ r \ n line change, ' Male '), Array (' 5 ', ' proud Snow star Maple,, ', ' Man '), Array (' 6 ', ' Proud Snow star ', ' Male '), Array (' 7 ', ' Snow star maple ', ' male '), array (' 8 ', ' Snow star maple ', ' male '), Array (' 9 ', ' Proud snow star Maple ', ' male
'), Array (' 10 ', ' 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 the record of each batch * @param int $offset offset * @param int $limit Get the number of record bars * @return Array * * Protected functi
On 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 ();
?>
Full instance code click here to download the site.
I hope this article will help you with your PHP program design.