Abstract class for PHP export CSV table

Source: Internet
Author: User
Abstract class for PHP export CSV table

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

 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->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 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

 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 ();? >
  • Related Article

    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.