How to Import the data from an excel sheet to the MySQL table AUTOMATICALLY?

断了今生、忘了曾经 提交于 2019-12-25 04:31:04

问题


I got a requirement to develop a Java app to load the data from an excel sheet to a MySQL database table everyday.

My actual requirement is when ever the user opens the application, it should automatically load the data from Excel -> MySQL database table.

I do not have idea to import the data from excel to MySQL table.

Can anyone help me in this?

Thanks in advance.


回答1:


You can import the data from excel using MySQL LOAD DATA INFILE command..

For automation you have to write a function to run the MySQL command on load..




回答2:


Create a dummyTable for daily load

Clear it before you begin

Save your excel in same column structure as dummyTable in csv format, comma delimited

Run:

LOAD DATA INFILE '/path/theFile1.csv' 
INTO TABLE dummyTable
    FIELDS TERMINATED BY ',' 
           OPTIONALLY ENCLOSED BY '"'
    LINES  TERMINATED BY '\n'

Proceed to use it (dummyTable). Clear it.




回答3:


Yes, you can generate the mysql queries from excel using PHP.

error_reporting(E_ALL ^ E_NOTICE);
require_once 'query_generator.php';
$data = new Spreadsheet_Excel_Reader("example.xls");

Output:

INSERT INTO table_name VALUES ( '101', 'Narendra Modi', 'Cabinet Ministers', 'Personnel, Public Grievances and Pensions, Department of Atomic Energy, Department of Space, All important policy issues and all other portfolios not allocated to any Minister', 'NULL', 'NULL', 'NULL');
INSERT INTO table_name VALUES ( '102', 'Rajnath Singh', 'Cabinet Ministers', 'Home Affairs', 'NULL', 'NULL', 'NULL');
INSERT INTO table_name VALUES ( '103', 'Sushma Swaraj', 'Cabinet Ministers', 'External Affairs, Overseas Indian Affairs', 'NULL', 'NULL', 'NULL');
INSERT INTO table_name VALUES ( '104', 'Arun Jaitley', 'Cabinet Ministers', 'Finance, Corporate Affairs and Defence', 'NULL', 'NULL', 'NULL');
......

I have written PHP code for generate all queries from excel with sngle click for our production system



来源:https://stackoverflow.com/questions/30907943/how-to-import-the-data-from-an-excel-sheet-to-the-mysql-table-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!