PHP XLS/CSV Parser
PHP May 19th, 2008Recently I have been writing an XLS/CSV parser in PHP for inserting records into a database. I wanted to implement a way to upload either a XLS/CSV file and for the data to be parsed and imported. Here is my implementation.
Implementation
For the CSV parser I used parseCSV:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php # include parseCSV class. # create new parseCSV object. $csv = new parseCSV(); $csv->auto('upload/'.$_FILES["file"]["name"]); ?> <table border="0" cellspacing="1" cellpadding="3"> <tr> <?php foreach ($csv->titles as $value): ?> <th><?php echo $value; ?></th> <?php endforeach; ?> </tr> <?php foreach ($csv->data as $key => $row): ?> <tr> <?php foreach ($row as $value): ?> <td><?php echo $value; ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </table> ?> |
For the XLS parser I used PHP-ExcelReader.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php // ExcelFile($filename, $encoding); $data = new Spreadsheet_Excel_Reader(); // Set output Encoding. $data->setOutputEncoding('CP1251'); $data->read('upload/'.$_FILES["file"]["name"]); ?> <table border="0" cellspacing="1" cellpadding="3"> <?php for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { ?> <tr> <?php for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { ?> <td><?php echo $data->sheets[0]['cells'][$i][$j]; ?></td> <?php } ?> </tr> <?php } ?> </table> |
Download
Click here to download the source.
June 16th, 2008 at 8:07 am
I tryed, but i couldn’t manage to put it working…
all i have is this:
“; $extension = strtolower(end(explode(’.', $nsf))); if (!in_array($extension, array(’xls’, ‘csv’))) { exit(’Invalid extension.’); } else if($_FILES["file"]["size"] 0) { echo “Return Code: ” . $_FILES["file"]["error"] . ”
“; } else { $pieces = explode(”.”, $_FILES["file"]["name"]); $importFilename = $pieces[0].”-”.date(”m.d.y”).”.”.$extension; if (file_exists(”upload/” . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . ” already exists. Overwritting….
“; } move_uploaded_file($_FILES["file"]["tmp_name"], “../upload/”.$importFilename ); echo “Stored as: ” . “/upload/”.$importFilename ; if($extension == “csv”) { include(’csv_parser.php’); } else if($extension == “xls”) { include(’xls_parser.php’); } } } ?>
If you know what is wrong please help me…
June 16th, 2008 at 10:06 am
fredo,
What errors if any is it showing you?
June 17th, 2008 at 1:49 am
when i click “submit” to upload the file, all i can see in the page is what i posted above.
June 17th, 2008 at 9:58 am
Fredo,
mmm ok, thats not normal. I updated a few things in the source so try download it again and let me know if you still have problems. Another thing it could be a problem with your PHP setup.
Create a file called phpinfo.php and inside it put the code:
Make sure a information page is shown correctly about your PHP environment.
June 17th, 2008 at 11:33 am
Now it is working!
Thank you very very much!
July 2nd, 2008 at 10:11 pm
hi,
I have an utf16 csv file and start the upload.html file and the upload works well, but when is the csv file beeing parsed? It doesn’t show something so i assume i have to do but don’t know what and how. Thanks Aldi
July 3rd, 2008 at 10:08 pm
Aldi,
The CSV file is parsed by the auto method. It is stored in memory by the function into a variable called $csv. The for loops iterate through each row of the CSV to output the data into a table. Email me if you need some more help. Thanks Andrew
July 7th, 2008 at 4:14 am
Hi,
i have one more problem.
Notice: iconv() [function.iconv]: Detected an illegal character in input string in C:\xampp\htdocs\fg\start\lib\reader.php on line 796
I tryed, but i still dont know where the problem is…
Thank you
July 7th, 2008 at 8:57 am
What version of PHP are you using?
July 7th, 2008 at 9:00 am
PHP version - 5.2.5
July 10th, 2008 at 9:13 pm
I have same problem but if I used only English alphabet everything is ok.
July 10th, 2008 at 9:42 pm
Hi,
Change from CP1251 to UTF-8 and it`s work for me.
July 12th, 2008 at 3:28 am
Hi Bojan,
Thanks a lot! Now it works fine!
July 25th, 2008 at 8:41 pm
Hi Andrew,
Thanks for the parser. Made my life much easier. I have a quick question though.
Is it possible for you to give me the php code insertion so that I can have the rows show with alternate background color (zebra).
I think, that is quite useful and makes life easier. I know I can do that with jquery or javascript. But I am using the table in an ajax tab and with ajax, javascript doesnt always work as it should.
I know that there is a simple if, else added somewhere in that code which would easily take care of it. Just dont know where and how.
I appreciate if you can help.
July 27th, 2008 at 7:14 pm
Uninitialized string offset: -512 in /home/platne/gto/public_html/moyodo/mtj/lib/oleread.inc on line 27
and
Undefined index: in /home/platne/gto/public_html/moyodo/mtj/lib/oleread.inc on line 138
August 22nd, 2008 at 2:09 am
Sol-
In xls_parser.exe, change:
<?php for ($i = 1; $i sheets[0]['numRows']; $i++) { ?>
<?php for ($j = 1; $j sheets[0]['numCols']; $j++) { ?>
sheets[0]['cells'][$i][$j]; ?>
to:
<?php for ($i = 1; $i sheets[0]['numRows']; $i++) { ?>
<tr bgcolor=”">
<?php for ($j = 1; $j sheets[0]['numCols']; $j++) { ?>
sheets[0]['cells'][$i][$j]; ?>
September 20th, 2008 at 6:39 pm
Can someone help with dates. Firslty, when I read a date it reads plus one day for instance, instead of showing 8/4/2008 it shows 8/5/2008. Secondly, if I set in the excel the format of the date to this 4-Aug-08, then I get this value 39664. Any help would be really appreciated
October 9th, 2008 at 4:33 pm
Hooltay: i have the same problem then you have.
Could you resolved it?
December 22nd, 2008 at 5:18 pm
Awsum…Really works fine…
Gudwork….
Gudluck ahead….