您的位置:首页 > 编程语言 > PHP开发

php生成csv文件的两个类

2007-10-18 09:05 253 查看
<?php
require_once 'fileOperation.class.php';
class csvHelper extends fileOperation{
var $mSpace = ',';
var $mHead;
var $mBody;
function addHeader($head=array()){
if (is_array($head)){
$this->mHead=implode(',',$head)."/r/n";

}
}
function addBodyData($body=array()){
if(is_array($body)){
for($i=0;$i<count($body);$i++){
$childBody=$body[$i];
for($j=0;$j<count($childBody);$j++){
$this->mBody.=$childBody[$j].$this->mSpace;
}
$this->mBody.="/r/n";
}
}

}
function _construct(){

}
function writeCSVDate(){
fwrite($this->mFp,$this->mHead.mb_convert_encoding($this->mBody,'sjis','sjis'));
}
function setSpace($type=','){
$this->mSpace=$type;
}
}

?>

class : fileOperation
<?php
class fileOperation {
var $fileName;
var $extendName='csv';
var $mPath='./';
var $mFp;
function fileOperation() {

}
function openFile($mode='w'){
if(empty($this->fileName)){
$this->setTimeFileName();
}
if (empty($this->extendName)){
$this->setExtendName();
}

$fp=fopen($this->mPath.'/'.$this->fileName.'.'.$this->extendName,$mode);
if($fp){
$this->mFp=$fp;
}else{
return 0;
}
}
function closeFile(){
return fclose($this->mFp);
}
function setTimeFileName($type='Ymd'){
if(!empty($type)){
$this->fileName=$type;
}else{
$this->fileName=time();
}
}
function setExtendName($extend='txt'){
if(!empty($extend)){
$this->extendName=$extend;
}else{
$this->extendName='.csv';
}
}
function setPath($path='./'){
$this->mPath=$path;
}
}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: