PHP data export to Execel class

Source: Internet
Author: User
Data

?
/*
Name:toexcel
Author:riyao Chen
version:0.0.1
Date:2006-05-15
License:gpl
*/

/****************************************
This class are 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 < Mysql_num_fields ($result)) {
$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 ();

*/
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.