찾다
php教程php手册PHP 数据导出到EXECEL类

数据


/*
    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();

*/
?>



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!