How to Dynamic Exporting to Excel by phpExcel

It is very common to have the list exportable to save live table. Doing this with online web list data is not that hard. We would like to recommend using phpExcel to get this work done. However, this tutorial not only introduce that library but also introduce how to using that library dynamically meaning that create your own library to work with the existing library.

Download PHPExcel library.

Code

<?php
    require_once './phpexcel/PHPExcel.php';
?>
<html>
    <head>
        <title>Demo Import Excel File</title>
    </head>
    <body>
        <h1>Table</h1>
        <table width="40%" border="1" cellspacing="0">
            <?php
            $i = 0;
            $file_type	= PHPExcel_IOFactory::identify("upload/client.xlsx");
            $objReader	= PHPExcel_IOFactory::createReader($file_type);
            $objPHPExcel    = $objReader->load("upload/client.xlsx");
            $sheet_data	= $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

            foreach ($sheet_data as $row)
            {
            ?>
                    <tr>
                        <td>
                            <?php
                                if($i == 0)
                                {
                                    echo "#";
                                    $i++;
                                }
                                else 
                                {
                                    echo $i++;
                                }
                            ?>
                        </td>
                        <td><?=$row['A'];?></td>
                        <td><?=$row['B'];?></td>
                        <td><?=$row['C'];?></td>
                        <td><?=$row['D'];?></td>
                    </tr>
            <?php }
            ?>
        </table>
    </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *