How to import an excel file into a MySQL database using PHP

Excel to MySQL illustration

I will provide basic code that’s needed to turn Excel or LibreOffice Calc file (.xlsx, .xls, .csv, .ods and some other formats) into a MySQL table with all the corresponding fields and data. This code will assume the first row of the document contains (unique) column names. That’s pretty much the only requirement.

First thing we need is PHPExcel package (it’s free and open source) for reading spreadsheet files. You can download it here: Download, extract and move it a folder where you can execute php files. We will include them in the following scripts. Continue reading