I want to import data from excel file using PHP and then if possible, save it to a MySQL database.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Quite possible. You can save your Excel file as a CSV file, and use fgetcsv() to read that file in to PHP.
fgetcsv()
will parse your data into an array, which you can then create SQL queries out of to put into your database.If all you're doing is putting it into a database, you might be able to bypass the need for a PHP script entirely and just use MySQL's
LOAD DATA INFILE
syntax on your CSV file instead:Here's a tutorial on reading/writing an Excel spreadsheet directly (without having to export to CSV). The necessary packages are available from SourceForge and PEAR (cf. article).
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
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.
Best bet is to export from Excel to a CSV (Comma separated values) file. These files are easy to parse and load. If you are reading directly from an XLS file, I'm not sure how to do that. You might want to look and see if there is a libarary for PHP that can read Excel data files.
Source