php導(dǎo)出CSV抽象類實(shí)例
本文實(shí)例講述了php導(dǎo)出CSV抽象類及其應(yīng)用,分享給大家供大家參考。具體分析如下:
該php導(dǎo)出CSV抽象類,可根據(jù)總記錄數(shù)與每批次記錄數(shù),計算總批次,循環(huán)導(dǎo)出。避免內(nèi)存不足的問題。
ExportCSV.class.php類文件如下:
<?php
/** php Export CSV abstract class,根據(jù)總記錄數(shù)與每批次記錄數(shù),計算總批次,循環(huán)導(dǎo)出。
* Date: 2014-05-16
* Author: fdipzone
* Ver: 1.0
*
* Func:
* public setPageSize 設(shè)置每批次導(dǎo)出的記錄條數(shù)
* public setExportName 設(shè)置導(dǎo)出的文件名
* public setSeparator 設(shè)置分隔符
* public setDelimiter 設(shè)置定界符
* public export 執(zhí)行導(dǎo)出
* private getPageCount 計算導(dǎo)出總批次
* private setHeader 設(shè)置導(dǎo)出文件header
* private formatCSV 將數(shù)據(jù)格式化為csv格式
* private escape 轉(zhuǎn)義字符串
* abstract getExportTotal 獲取總記錄條數(shù),抽象方法,需繼承類實(shí)現(xiàn)
* abstract getExportFields 獲取導(dǎo)出的列名,抽象方法,需繼承類實(shí)現(xiàn)
* abstract getExportData 獲取每頁記錄,抽象方法,需繼承類實(shí)現(xiàn)
*/
abstract class ExportCSV{ // class start
// 定義子類必須要實(shí)現(xiàn)的方法
/** 獲取總記錄條數(shù)
* @return int
*/
abstract protected function getExportTotal();
/** 獲取導(dǎo)出的列名
* @return Array
*/
abstract protected function getExportFields();
/** 獲取每批次數(shù)據(jù)
* @param int $offset 偏移量
* @param int $limit 獲取的記錄條數(shù)
* @return Array
*/
abstract protected function getExportData($offset, $limit);
// 定義類屬性
protected $total = 0; // 總記錄數(shù)
protected $pagesize = 500; // 每批次導(dǎo)出的記錄數(shù)
protected $exportName = 'export.csv'; // 導(dǎo)出的文件名
protected $separator = ','; // 設(shè)置分隔符
protected $delimiter = '"'; // 設(shè)置定界符
/** 設(shè)置每次導(dǎo)出的記錄條數(shù)
* @param int $pagesize 每次導(dǎo)出的記錄條數(shù)
*/
public function setPageSize($pagesize=0){
if(is_numeric($pagesize) && $pagesize>0){
$this->pagesize = $pagesize;
}
}
/** 設(shè)置導(dǎo)出的文件名
* @param String $filename 導(dǎo)出的文件名
*/
public function setExportName($filename){
if($filename!=''){
$this->exportName = $filename;
}
}
/** 設(shè)置分隔符
* @param String $separator 分隔符
*/
public function setSeparator($separator){
if($separator!=''){
$this->separator = $separator;
}
}
/** 設(shè)置定界符
* @param String $delimiter 定界符
*/
public function setDelimiter($delimiter){
if($delimiter!=''){
$this->delimiter = $delimiter;
}
}
/** 導(dǎo)出csv */
public function export(){
// 獲取總記錄數(shù)
$this->total = $this->getExportTotal();
// 沒有記錄
if(!$this->total){
return false;
}
// 計算導(dǎo)出總批次
$pagecount = $this->getPageCount();
// 獲取導(dǎo)出的列名
$fields = $this->getExportFields();
// 設(shè)置導(dǎo)出文件header
$this->setHeader();
// 循環(huán)導(dǎo)出
for($i=0; $i<$pagecount; $i++){
$exportData = '';
if($i==0){ // 第一條記錄前先導(dǎo)出列名
$exportData .= $this->formatCSV($fields);
}
// 設(shè)置偏移值
$offset = $i*$this->pagesize;
// 獲取每頁數(shù)據(jù)
$data = $this->getExportData($offset, $this->pagesize);
// 將每頁數(shù)據(jù)轉(zhuǎn)換為csv格式
if($data){
foreach($data as $row){
$exportData .= $this->formatCSV($row);
}
}
// 導(dǎo)出數(shù)據(jù)
echo $exportData;
}
}
/** 計算總批次 */
private function getPageCount(){
$pagecount = (int)(($this->total-1)/$this->pagesize)+1;
return $pagecount;
}
/** 設(shè)置導(dǎo)出文件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);
}
/** 格式化為csv格式數(shù)據(jù)
* @param Array $data 要轉(zhuǎn)換為csv格式的數(shù)組
*/
private function formatCSV($data=array()){
// 對數(shù)組每個元素進(jìn)行轉(zhuǎn)義
$data = array_map(array($this,'escape'), $data);
return $this->delimiter.implode($this->delimiter.$this->separator.$this->delimiter, $data).$this->delimiter."\r\n";
}
/** 轉(zhuǎn)義字符串
* @param String $str
* @return String
*/
private function escape($str){
return str_replace($this->delimiter, $this->delimiter.$this->delimiter, $str);
}
} // class end
?>
demo示例程序如下:
<?php
// ExportCSV abstract class
require "ExportCSV.class.php";
// 定義繼承類
class myexport extends ExportCSV{
// 要導(dǎo)出的數(shù)據(jù),實(shí)際情況會從db讀取
protected $data = array(
array('1','傲雪星楓"','男'),
array('2','傲雪星楓","','男'),
array('3','傲雪星楓","','男'),
array('4',"傲雪星楓\"\"\r\n換行",'男'),
array('5','傲雪星楓,,','男'),
array('6','傲雪星楓"','男'),
array('7','傲雪星楓','男'),
array('8','傲雪星楓','男'),
array('9','傲雪星楓','男'),
array('10','傲雪星楓','男')
);
/* 返回總導(dǎo)出記錄數(shù)
* @return int
*/
protected function getExportTotal(){
return count($this->data);
}
/** 返回導(dǎo)出的列名
* @return Array
*/
protected function getExportFields(){
$title = array('id','name','gender');
return $title;
}
/* 返回每批次的記錄
* @param int $offset 偏移量
* @param int $limit 獲取的記錄條數(shù)
* @return Array
*/
protected function getExportData($offset, $limit){
return array_slice($this->data, $offset, $limit);
}
}
// 導(dǎo)出
$obj = new myexport();
$obj->setPageSize(1);
$obj->setExportName('myexport.csv');
$obj->setSeparator(',');
$obj->setDelimiter('"');
$obj->export();
?>
完整實(shí)例代碼點(diǎn)擊此處本站下載。
希望本文所述對大家的PHP程序設(shè)計有所幫助。
- thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
- 基于php導(dǎo)出到Excel或CSV的詳解(附utf8、gbk 編碼轉(zhuǎn)換)
- PHP導(dǎo)出MySQL數(shù)據(jù)到Excel文件(fputcsv)
- 詳解PHP導(dǎo)入導(dǎo)出CSV文件
- php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例
- php導(dǎo)出csv格式數(shù)據(jù)并將數(shù)字轉(zhuǎn)換成文本的思路以及代碼分享
- PHP 導(dǎo)出數(shù)據(jù)到淘寶助手CSV的方法分享
- PHP實(shí)現(xiàn)CSV文件的導(dǎo)入和導(dǎo)出類
- PHP 實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出到.csv文件方法
- php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法
- 原生PHP實(shí)現(xiàn)導(dǎo)出csv格式Excel文件的方法示例【附源碼下載】
相關(guān)文章
PHP查找數(shù)組中只出現(xiàn)一次的數(shù)字實(shí)現(xiàn)方法【查找特定元素】
這篇文章主要介紹了PHP查找數(shù)組中只出現(xiàn)一次的數(shù)字實(shí)現(xiàn)方法,涉及php使用array_count_values針對數(shù)組元素進(jìn)行統(tǒng)計的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
PHP判斷IP并轉(zhuǎn)跳到相應(yīng)城市分站的方法
這篇文章主要介紹了PHP判斷IP并轉(zhuǎn)跳到相應(yīng)城市分站的方法,實(shí)例分析了php解析URL及跳轉(zhuǎn)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
高質(zhì)量PHP代碼的50個實(shí)用技巧必備(下)
這篇文章主要為大家分享了50個高質(zhì)量PHP代碼的實(shí)用技巧,大家必備的php實(shí)用代碼,感興趣的小伙伴們可以參考一下2016-01-01
php下利用curl判斷遠(yuǎn)程文件是否存在的實(shí)現(xiàn)代碼
php下利用curl判斷遠(yuǎn)程文件是否存在的實(shí)現(xiàn)代碼,我們有些程序因需要事先判斷文件是否存在然后再進(jìn)行后面的操作。2011-10-10
php實(shí)現(xiàn)掃描二維碼根據(jù)瀏覽器類型訪問不同下載地址
本程序?qū)崿F(xiàn)的功能就是掃描同一個二維碼根據(jù)瀏覽器類型訪問不同下載地址,很實(shí)用的,建議大家學(xué)習(xí)下2014-10-10
php實(shí)現(xiàn)使用正則將文本中的網(wǎng)址轉(zhuǎn)換成鏈接標(biāo)簽
本文給大家分享一段php中使用正則表達(dá)式將網(wǎng)址轉(zhuǎn)換成A鏈接的函數(shù)代碼,十分簡潔實(shí)用,這里推薦給大家2014-12-12

