Safety Shop's Blog

You come when you need, away when you get

PHP Excel Export class

By • Feb 3rd, 2009 • Category: Web Programming

The class has a download() function that will send the appropriate headers for a binary download. Everybody knows phpMyAdmin can export file to Excel format but phpMyAdmin just export .csv file,not real Excel file format. If you are interest in PHP programming and need to export to the real Excel format please check it out

Download PHP XLS Class here!excelexport.zip

How to Use?

require_once "ExcelExport.php";

$xls = new ExcelExport();

$xls->addRow(Array("First Name","Last Name","Website","ID"));

$xls->addRow(Array("james","lin","www.chumby.net",0));

$xls->addRow(Array("bhaven","mistry","www.mygumballs.com",1));

$xls->addRow(Array("erica","truex","www.wholegrainfilms.com",2));

$xls->addRow(Array("eliot","gann","www.dissolvedfish.com",3));

$xls->addRow(Array("trevor","powell","gradius.classicgaming.gamespy.com",4));

$xls->download("websites.xls");

?>

If you want to use with Mysql Query, it’s something like this:


?>
$server = "localhost"; // server to connect to.
$database = "database"; // the name of the database.
$db_user = "username"; // mysql username to access the database with.
$db_pass = "password"; // mysql password to access the database with.

$connect = mysql_connect($server,$db_user,$db_pass) or die(mysql_error());
if($connect) {
//select database
$select = mysql_select_db($database);
}

$ambil = mysql_query("SELECT * FROM database where id < 100 order by id asc");
$xls->addRow(Array(No,Date,City,Category,Title));
$i=1;
while($data = mysql_fetch_array($ambil)) {
$xls->addRow(Array($i,date("d-m-Y H:i:s",$data['id']),$data['city']),category($data['category']),$data['title']));
$i++;
}

$xls->download("Statistic.xls");
?>

2 Responses »

  1. Thanks for The Tutorials.

  2. That is a great article. I really think it will work out to be a valuable piece of information in the future for me.

Leave a Reply