Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Data from Excel in PHP [closed]

I want to import data from excel file using PHP and then if possible, save it to a MySQL database.

like image 471
air Avatar asked Jan 14 '10 19:01

air


People also ask

How can read data from Excel file in PHP?

Read Excel File First, import the needed library and load the Reader of XLSX. Read the excel file using the load() function. Here test. xlsx is the file name.


1 Answers

Importing from Excel files (XLS) is way harder than improting from CSV files. Usually I save my XLS to CSV with Excel then work on this CSV with PHP...

Look at PHP function fgetcsv at: http://ca.php.net/manual/en/function.fgetcsv.php

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?> 

If you still want to load XLS directly from PHP it's possible (but how reliable)... A quick seach resulted in http://sourceforge.net/projects/phpexcelreader/ which might be helpful.

like image 97
AlexV Avatar answered Oct 19 '22 23:10

AlexV