[Program] Read xls files with PHP

No Gravatar

Like most of my post even this comes not only with the intent to do some 'information, to remind me of how I have solved a problem that occurred to me: In fact my memory is usually fallacious and googol To find the same information more than once I do not like very much ^ _ ^

An activity that often we have to do is to process "on the fly" a xls file (perhaps as a result dell'upload available to a user) and manage the data contained in it (possibly structured as we expected otherwise the thing is impractical ..) for example, the popular tables in a database or simply view them on our site.

Well, the solution to this problem is easier than you think.

It runs a project on Sourceforge that makes it available via the class ExcelReader everything we need.

Having included the two files reader.php and oleread.inc in our script and we will be ready to do the job "dirty".

First we initialize our class and then passiamogli the file to open:

  $ data-> read ( 'nome_filel.xls'); 

The next thing to do, of course, is to make a loop for each row and column pointing out that:

  $ data-> sheets [0] [ 'numRows'];
 $ data-> sheets [0] [ 'numCols']; 

represent the number of rows and columns in the file xls (particularly in the first sheet of paper).
At this point we can take the value contained in each cell access it as we do with a trivial matrix:

  $ data-> sheets [0] [ 'cells'] [$ i] [$ j]; 

A sample script to show a video data from a xls file in tabular form are the following:

  <?
 require_once 'Excel / reader.php';
 $ data = new Spreadsheet_Excel_Reader ();
 $ data-> setOutputEncoding ( 'CP1251');
 $ data-> read ( 'nome_file.xls');
 error_reporting (E_ALL ^ E_NOTICE);

 echo "<table>";
 for ($ i = 1, $ i <= $ data-> sheets [0] [ 'numRows'], $ i + +) (
         echo "<tr>";
	 for ($ j = 1, $ j <= $ data-> sheets [0] [ 'numCols'], $ j + +) (
		 echo "<td>. $ data-> sheets [0] [ 'cells'] [$ i] [$ j ]."</ td>";
	 )
	 echo "</ tr>";
 )
 echo "</ table>";
 ?> 

PS: Of course you can also loop on the sheets that compose the file .. xls

Reblog this post [with Zemanta]

Popularity: 8% [?]

Tags:


leave a response , or trackback from your own site. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

This website uses IntenseDebate comments, but they are not currently loaded Because either your browser does not support JavaScript, or they did not load fast enough.

Leave a Reply