Heim > php教程 > php手册 > PHP 数据导出到EXECEL类

PHP 数据导出到EXECEL类

WBOY
Freigeben: 2016-06-21 09:07:19
Original
997 Leute haben es durchsucht

数据


/*
    Name: ToExcel
    Author: Riyao Chen
    Version: 0.0.1
    Date: 2006-05-15
    License: GPL
*/

/****************************************
//This class is to change data from query to excel file
//SQL FORMAT:
 SELECT c_id,c_title,c_subject FROM news_content WHERE c_parid=0 ORDER BY c_date desc,c_clicks desc
//The DataBase connection is open outside;
//Parameter:$sql,The query sentence ;$database,which database
***********************************/
class ToExcel
{
 var $sql="";
 var $fields;

 var $db;
 
 function ToExcel($sql,$database)
 {
  if($sql == "")
   return $this->errorMessage("NO QUERY SENTENCE!");

  $this->db = $database;
  
  //Get Table Name 
  $tmp=ereg_replace("SELECT.+FROM ","",$sql);
  $tmp=ereg_replace("ORDER BY.+","",$tmp);
  $this->table = ereg_replace(" WHERE.+","",$tmp);
  //Get Fields 
  $field = ereg_replace("SELECT ","",$sql);
  $field = ereg_replace(" FROM.+","",$field);
  if(trim($field) == "*")
   $this->fields = $this->GetFieldList($this->table);//$mysql->GetFieldList($this->table);
  else
   $this->fields = explode(",",$field);
   
  $this->sql = $sql;
  
  }
 
 function ShowExcel()
 {  
  header("Content-type:application/vnd.ms-excel");
  header("Content-Disposition:filename=Excel.xls");
  
  //OutPut Fields Start
  foreach($this->fields as $key=>$value)
  {
   echo $value."\t";
   
   }
  echo "\n";
  //OutPut Fields End
  
  //OutPut Field Value Start   
  $result = mysql_query($this->sql);
  while($row = mysql_fetch_array($result))
  {
   foreach($this->fields as $key=>$value)
    echo iconv("utf-8","gb2312",$row[$value]."\t");

   echo "\n";
   }
   
  //OutPut Field Value End
  
  }
 
 //Get The FieldLis
 function GetFieldList($table)
 {
  if($result=mysql_list_fields($this->db,$table)){
             $i=0;
             while($i                  $fd_names[$i]=mysql_field_name($result,$i);
                 $i++;
             }
             return($fd_names);
             
         }else
              return $this->errorMessage("Unable to find any field list in table: $tbl_name");

  }
 
  function errorMessage($msg){
         echo "Error: $msg : ".mysql_error();
  return false;
      }
  
 }
 

/*
exemple

$sql = "SELECT * FROM news_content WHERE c_parid=0 ORDER BY c_date desc,c_clicks desc";
$excel = new ToExcel($sql,DATABASE_NAME);
$excel->ShowExcel();

*/
?>



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage