phpeveryday.com

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


PHP Excel Report: Handling Data From Excel

Tag: excel, xml   Category: PHP Basic
post: 30 Dec 2007 read: 1,792


PHP Excel Report tutorial: Excel is still the most popular spreadsheet. Your new customer maybe only have data in excel format. Your job is import that data to database. This post is about trick how to read data from excel.

For first time, we (or your client) must save excel in XML format. Following screen show how to save excel it XML format at Microsoft Excel 2003.

You will get Save As dialog box.

Now, we need a form to upload this file in web page. Create a file named "upload.php" within www/test/excel. Enter following codes:


 <html>
 <body>
 <form enctype="multipart/form-data" action = "handledata.php" method="post">
 	 Upload a Excel XML file:
 	<input type="file" name="ufile" />
     <br/>
 	<input type="submit" value="Upload" />
 </form>
 </body>
 </html>

Now, we create a file name "handledata.php" within www/test/excel. Enter following code:


 <html>
 <body>
 <?php
 $data = array();
 if ( $_FILES['ufile']['tmp_name'] )
 {
 	
 	$dom = DOMDocument::load( $_FILES['ufile']['tmp_name'] );
 	$rows = $dom->getElementsByTagName( 'Row' );
 	foreach ($rows as $row)
 	{
 		$cells = $row->getElementsByTagName( 'Cell' );
 		$datarow = array();
 		foreach ($cells as $cell)
 		{
 			$datarow []= $cell->nodeValue;
 		}
 		$data []= $datarow;
 			
 	}
 }
 ?>
 <table>
 <?php foreach( $data as $row ) { ?>
 <tr>
 <?php foreach( $row as $item ) { ?>
 <td><?php echo( $item ); ?></td>
 <?php } ?>
 </tr>
 <?php } ?>
 </table>
 </body>
 </html>



| 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