Load an .xlsx file into mysql table or convert it to .csv in PHP

Wednesday, November 4, 2015

I have an excel file (.xlsx) with 2 columns (date, number).
I have a mysql table with 2 columns (date, number) where number is decimal.



I am trying to load this file into my table in mysql.



The first column (date) is in a date format : %d/%m/%Y but I want it to be : %Y-%m-%d.



The second column is in an Accounting format.



I am using the following code :



$Db->query('LOAD DATA LOCAL INFILE \'/file.xlsx\'  
INTO TABLE '.$in_table.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 8 ROWS
(@date, number)
set date=str_to_date(@date,\'%Y-%m-%d\')
;');


01) It does not upload the dates because of my set. How can I upload them in the right format and convert it in the load query without using update?



02) It does not upload the number cells.



03) Additionally, I want to use this code :



$Db->query('delete from '.$in_table.' 
where date is null or number = 0
;');


in order to delete the rows that do not start with a date format and also the rows where the number is empty. Here is the excel file in a printscreen :



enter image description here



So, according to (03), rows 32,33 and 34 should not be included in my sql table.



I want to load or either convert the .xlsx to .csv if I have to. I want to do it either in php or sql code (in a php file). I tried many versions but they are all wrong.

0 comments:

Post a Comment