The code below can be directly used to generate a dynamic excel by just modifying the quesy.
File 1: class.database.php
<?php
// Class : begin
class Database
{
var $host;
var $password;
var $user;
var $database;
var $link;
var $query;
var $result;
var $rows;
function Database()
{ // Method : begin
$this->host = "";
$this->password = "";
$this->user = "";
$this->database = "";
$this->rows = 0;
} // Method : end
function OpenLink()
{ // Method : begin
$this->link = @mysql_connect($this->host,$this->user,$this->password) or die (print "Class Database: Error while connecting to DB (link)");
} // Method : end
function SelectDB()
{ // Method : begin
@mysql_select_db($this->database,$this->link) or die (print "Class Database: Error while selecting DB");
} // Method : end
function CloseDB()
{ // Method : begin
mysql_close();
} // Method : end
function Query($query)
{ // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query :: $query.");
// $rows=mysql_affected_rows();
if(ereg("SELECT",$query))
{
$this->rows = mysql_num_rows($this->result);
}
$this->CloseDB();
} // Method : end
function Query1($query)
{ // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query :: $query.");
return $this->result;
} // Method : end
}
// Class : end
?>
File 2: class.excel.php
<?php
<?php
// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
function HeaderingExcel($filename) {
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=" . $filename . ".xls");
header("Content-type: application/msexcel");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Transfer-Encoding: binary ");
}
?>
File3: ExcelReport.php
<?php
require_once("class.database.php");
require_once("class.excel.php");
$database=new database();
$sql="select * from employee";
$result = $database->Query1($sql);
$numfields = mysql_num_fields($result);
xlsBOF();
$r=1;
for ($i = 0; $i<$numfields; $i += 1) {
$field = mysql_fetch_field($result, $i);
xlsWriteLabel(0,$i,$field->name);
}//for
while ($fielddata = mysql_fetch_array($result)) {
for ($i = 0; $i<$numfields; $i += 1) {
$field = mysql_fetch_field($result, $i);
if($fielddata[$field->name] != "")
{
xlsWriteLabel($r,$i,strip_tags($fielddata[$field->name]));
}
else
{
xlsWriteLabel($r,$i,"");
}
}//for
$r++;
}//while
xlsEOF();
exit();
?>
File 1: class.database.php
<?php
// Class : begin
class Database
{
var $host;
var $password;
var $user;
var $database;
var $link;
var $query;
var $result;
var $rows;
function Database()
{ // Method : begin
$this->host = "";
$this->password = "";
$this->user = "";
$this->database = "";
$this->rows = 0;
} // Method : end
function OpenLink()
{ // Method : begin
$this->link = @mysql_connect($this->host,$this->user,$this->password) or die (print "Class Database: Error while connecting to DB (link)");
} // Method : end
function SelectDB()
{ // Method : begin
@mysql_select_db($this->database,$this->link) or die (print "Class Database: Error while selecting DB");
} // Method : end
function CloseDB()
{ // Method : begin
mysql_close();
} // Method : end
function Query($query)
{ // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query :: $query.");
// $rows=mysql_affected_rows();
if(ereg("SELECT",$query))
{
$this->rows = mysql_num_rows($this->result);
}
$this->CloseDB();
} // Method : end
function Query1($query)
{ // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query :: $query.");
return $this->result;
} // Method : end
}
// Class : end
?>
File 2: class.excel.php
<?php
<?php
// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
function HeaderingExcel($filename) {
header("Expires: 0");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=" . $filename . ".xls");
header("Content-type: application/msexcel");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Transfer-Encoding: binary ");
}
?>
File3: ExcelReport.php
<?php
require_once("class.database.php");
require_once("class.excel.php");
$database=new database();
$sql="select * from employee";
$result = $database->Query1($sql);
$numfields = mysql_num_fields($result);
xlsBOF();
$r=1;
for ($i = 0; $i<$numfields; $i += 1) {
$field = mysql_fetch_field($result, $i);
xlsWriteLabel(0,$i,$field->name);
}//for
while ($fielddata = mysql_fetch_array($result)) {
for ($i = 0; $i<$numfields; $i += 1) {
$field = mysql_fetch_field($result, $i);
if($fielddata[$field->name] != "")
{
xlsWriteLabel($r,$i,strip_tags($fielddata[$field->name]));
}
else
{
xlsWriteLabel($r,$i,"");
}
}//for
$r++;
}//while
xlsEOF();
exit();
?>
No comments:
Post a Comment