phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


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>.
  1. Download and extract to a directory (in this tutorial: practice).
  2. Create a new file "report.php" at the same directory.
  3. 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
  4. 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();
    ?>
    
    
  5. Now, execute report.php. And then click link Excel. You will get dialog box like below.
  6. Klik Open. Your speadsheet editor will opened, and You will see result like below:
Next, we will discuss about text position


Series this article:
Write report in Excel Format (Part 1)
PHP Excel Report: Text Position in Excel Cell
PHP Excel Report: Formating Cell
PHP Excel Report: Creating Several Worksheets
PHP Excel Report: Type of Text

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting