Write report in Excel Format (Part 1)Tag: Excel, PHP Classes Category: PHP Classes post: 05 Oct 2007 read: 3,441
PHP Excel Report Tutorial: I will talk about wonderfull class that very usefull for writing reports. Yes, as you look at title, we will try writing report in Excel format. What is Benefit? Yup, you can say to your client, by using Excel format, they can customize that report as they like. Stop! Is it difficult? Mmm.. how about if I tell very difficult? Are you worried? He he he... No,No,No... Its easy man... Lets go we try!
First we need download wonderfull Excel class. You can downloat at . Module written/ported by Xavier Noguer <xnoguer@rezebra.com>.
- Download and extract to a directory (in this tutorial: practice).
- Create a new file "report.php" at the same directory.
- Then, we create data in html format. The code like this:
<html>
<head><title>Our Data</title></head>
<body>
<?
$arrData = array(array('No','Name'),
array('1','Ilmia'),
array('2','Aqila'));
?>
<table border="1">
<?
for($i=0;$i<=count($arrData)-1;$i++){
echo "<tr>";
for($j=0;$j<=count($arrData[$i])-1;$j++){
echo "<td>";
echo $arrData[$i][$j];
echo "</td>";
}
echo "</tr>";
}
?>
</table>
<br>
Download this report as
<a href="test1.php"
mce_href="test1.php">Excel</a>
</body>
</html>
Assuming, your directory at test/excel. Point your browser to http://localhost/test/exce/report.php. The result look like this

- Next, we create file that generate excell report. The file, we called "test1.php". The code below:
<?php
require_once('OLEwriter.php');
require_once('BIFFwriter.php');
require_once('Worksheet.php');
require_once('Workbook.php');function HeaderingExcel($filename) {
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename" );
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
}// HTTP headers
HeaderingExcel('test.xls');// Creating a workbook
$workbook = new Workbook("-");
// Creating the first worksheet
$worksheet1 =& $workbook->add_worksheet('First One');
$worksheet1->write_string(1, 1, "This worksheet's name is ".$worksheet1->get_name());
// data
$arrData = array(array('No','Name'),
array('1','Ilmia'),
array('2','Aqila'));
for($i=0;$i<=count($arrData)-1;$i++){
for($j=0;$j<=count($arrData[$i])-1;$j++){
$worksheet1->write_string(2+$i, 1+$j,$arrData[$i][$j]);
}
}
$workbook->close();
?>
- Now, execute report.php. And then click link Excel. You will get dialog box like below.
- Klik Open. Your speadsheet editor will opened, and You will see result like below:
Next, we will discuss about text position
|
|
| Give Your Opinion | Recommend
|