當前位置:編程學習大全網 - 源碼下載 - 請教壹下:用PHP 從MYsql 中查詢數據並導出成cvs 或者xls 怎麽實現。如果方便的話給個源代碼,尤為感激!

請教壹下:用PHP 從MYsql 中查詢數據並導出成cvs 或者xls 怎麽實現。如果方便的話給個源代碼,尤為感激!

csv很簡單

數據以,分開

行用\n

存為.csv就行了

生成xls的話稍微復雜壹點兒,下面是壹個生成xls的類,妳可以按照妳的需要修改調用壹下。

======================================

用PHP生成xls,csv格式文件的類

2008年09月23日 星期二 上午 09:56

fileOperation.php

****************************************************

class fileOperation {//基類

var $fileName='test';

var $extendName='csv';

var $mPath='./report/';

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;

}

}

xlsHelper.php

****************************************************

require_once 'fileOperation.php';

class xlsHelper extends fileOperation{//具體實現子類

var $mSpace = '';

var $mHead;

var $mBody='';

function addHeader($head=array()){

$this->mHead='<table width="500" border="1" align="center" cellpadding="5"><tr>';

if (is_array($head)){

foreach($head as $hd){

$this->mHead.='<th bgcolor="#A5A0DE">'.$hd.'</th>';

}

}

$this->mHead.='</tr>';

}

function addBodyData($body=array()){

if(is_array($body)){

for($i=0;$i<count($body);$i++){

$childBody=$body[$i];

$this->mBody.='<tr>';

$this->mSpace = '<td align="center">';

for($j=0;$j<count($childBody);$j++){

$this->mBody.=$this->mSpace.$childBody[$j].'</td>';

}

$this->mBody.="</tr>";

}

}

$this->mBody.='</table>';

}

function _construct(){

}

function writeCSVDate(){

return fwrite($this->mFp,$this->mHead.mb_convert_encoding($this->mBody,'sjis','sjis'));

}

function setSpace($type=','){

$this->mSpace=$type;

}

}

test.php

****************************************************

$xls=new xlsHelper();

$xls->fileName='xxx';//設置生成文件的文件名

$xls->extendName='xls';//文件擴展名

$xls->mPath='./';//文件保存路徑

$headerarr=array('姓名','年齡','郵箱');//頭部字段名

$xls->addHeader($headerarr);

$datasarr=array(//註意:此處的二維數組壹定要是數字索引

array('yht',20,'yht@163.com'),

array('ktv009',23,'ktv009@sina.com'),

);

$xls->addBodyData($datasarr);

$xls->openFile('w');

if($xls->writeCSVDate()) echo "<script language='javascript'>生成文件成功</script>";

else echo ""<script language='javascript'>無法生成文件</script>";

  • 上一篇:安卓ZXing二維碼,編解碼的算法是什麽,或者用位圖生成二維碼的算法是什麽?謝謝妳。
  • 下一篇:航海家入坑路之二,拉出去溜了溜的感受
  • copyright 2024編程學習大全網