Hi,
If you need to do large data migration from an excel sheet to your MySQL database, the most conventional way is to convert the .xls or .xlsx files to simple CSV(comma seperated file) and read it using PHP file read function. However we can also read a .xls file without converting it to a csv file. We can even navigate through the various worksheets in a single file. Click here to download the entire source code. I have used couple of external class files to achieve this.
Lets have a look at the spreadsheet I am using as example:
Code and Explanation:
First we need to include the main class file in our PHP page and initialise an object for it as follows:
include 'reader.php';
$excel = new Spreadsheet_Excel_Reader();
Now we are loading the excel file using the above created object as:
$excel->read('sample.xls');
Now we navigate through the rows and columns of the first worksheet in the excel file and display it as a simple HTML table in the browser:
$x=1;
while($x<=$excel->sheets[0]['numRows']) {
echo "\t<tr>\n";
$y=1;
while($y<=$excel->sheets[0]['numCols']) {
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
echo "\t\t<td>$cell</td>\n";
$y++;
}
echo "\t</tr>\n";
$x++;
}
In the above sheet[0] is used to read cells from the first work sheet, you can change it according to your needs. Now in sheets[0]['cells'], ['cells'] is a 2D array storing the data as shown in the above screen shot. After executing the entire source code, this is the output that is generated:
Lets have a look at the spreadsheet I am using as example:
Code and Explanation:
First we need to include the main class file in our PHP page and initialise an object for it as follows:
include 'reader.php';
$excel = new Spreadsheet_Excel_Reader();
Now we are loading the excel file using the above created object as:
$excel->read('sample.xls');
Now we navigate through the rows and columns of the first worksheet in the excel file and display it as a simple HTML table in the browser:
$x=1;
while($x<=$excel->sheets[0]['numRows']) {
echo "\t<tr>\n";
$y=1;
while($y<=$excel->sheets[0]['numCols']) {
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
echo "\t\t<td>$cell</td>\n";
$y++;
}
echo "\t</tr>\n";
$x++;
}
In the above sheet[0] is used to read cells from the first work sheet, you can change it according to your needs. Now in sheets[0]['cells'], ['cells'] is a 2D array storing the data as shown in the above screen shot. After executing the entire source code, this is the output that is generated:


nice dear i when i compile you source code it show following error please
ReplyDeleteDeprecated: Assigning the return value of new by reference is deprecated in C:\wamp\www\e\reader.php on line 26
hey! I got the same..
Deletei too got the error on line 261 as Deprecated: Assigning the return value of new by reference is deprecated
Deletejust removeng & character so the code :
Delete$this->_ole = new OLERead();
works! yeah!
ReplyDeleteGreat! it works.. Nice thanks
ReplyDeleteThanks a lot. Awesome code.....
ReplyDeleteExcellent piece of work. Thanks for posting.
ReplyDeleteThank's a lot.It's working good. Awesome codeing....
ReplyDeleteits working fine....
ReplyDeleteBut how can we read a date from from excel file because its does not return the exact date.
Thanks
Thank you sooooooooo much!!! Works Great!!!
ReplyDeleteHi,
ReplyDeleteThank for the code, it's working good,
Can you please help me to identify the headers in excel sheet, like name, country......
Hello, it is working fine with the excel file you attached with the code, but if i try it with xlsx format, it is saying it is not readable.
ReplyDeleteCan you help me with this.
The vendor which is used here, does not supports xlsx. Best way would be to convert the xlsx to xls and then use the code. I will try to post one for xlsx soon.
Deletehey please specify the path to define for the class in the projects!!!
ReplyDeleteWould b a great help
Sure. Will keep that in mind in future :)
DeleteIs there any way to get cell address as "A1" if i have row number and column number also?
ReplyDeleteYou probably need to write a separate script for finding that out. It is not a part of this package. But I am sure it cannot be that difficult.
Deletehi
ReplyDeleteLet me say this is a great work! But I found a weird thing today.
I have an excel which gets parsed perfectly only if i remove the last row. Any idea if the excel end marker is screwing the read function? I am not sure. I have been breaking head for a while now and any help in this regard would be much appreciated.
Thanks !
I guess you have checked whether special characters are stuffing things up. Try adding a fresh row replacing the one causing the issue and check.
DeleteYa its work but how make file name dynamic ?
ReplyDeletethanx!
to avoid the deprecate error on line261 just add error_reporting(0);
ReplyDeleteon the very frst line of your php script so that the errors will get suppressed and u will get only the output.
Hi how can i read excel file which has external link..
ReplyDeletelike http:sitename.co/filename?
like the document from dropbox.
I am not sure whether this package is capable of reading external links. But a quick solutions would be to read the file using fopen() and save it in your server, and then use the above given code of that local version file. Hope this helps!!!
DeleteGreat work. Thanks
ReplyDeletethanku so much, its working....
ReplyDeletehow can i edit excel sheet??
ReplyDeleteIs it possible to amend this code to make it work to read .xlsx files?
ReplyDeleteNo, this API is specfic to older version, .xls
Delete