简体   繁体   English

将大量Excel数据插入MySQL数据库

[英]Insert large amount of Excel data into a MySQL database

I have large amount of data in my excel sheet. 我的Excel工作表中有大量数据。 The excel sheet contains 5 to 6 tabs, and the columns are different in different tabs. Excel工作表包含5到6个标签,并且不同标签中的列不同。 Now I want to insert all data in database using array, but problem is due to no of columns. 现在我想使用数组将所有数据插入数据库,但是问题是由于没有列。 Is there any way to take columns name as array key name? 有什么办法可以将列名作为数组键名?

Here is my code: 这是我的代码:

$inserarray = array();

$inserarray['bl'][0] = "INSERT INTO  `new`.`bl` 
(`Code` ,`url` ,`date` ,`Bl` , `Title` ,`Sub`)";

$inserarray['bl'][1] = "VALUES ('1',  '1',  '12',  '12',  'ds',  's',  'sd',  's');";

$inser_query = $inserarray[$d1][0]; // [column_name][]

$process_insertarray[] = "('".implode("','",$projdetail)."','".implode("','",$sup)."')";

$final_query = $inser_query.''.'VALUES'.implode(',',$process_insertarray);

If it is large amount of data, I prefer exporting(save as) the excel data into CSV( or TSV) file and importing to DB. 如果数据量很大,我更喜欢将excel数据导出(另存为)到CSV(或TSV)文件中,然后导入到DB中。

Assume the exported file is called 'data.csv'. 假设导出的文件称为“ data.csv”。 You can import the file using this command in mysql command interpreter. 您可以在mysql命令解释器中使用此命令导入文件。

load data local infile 'data.csv' into table YOURTABLE

Make sure you create a table with the same number(and type) of columns in the excel, and fire! 确保在Excel中创建具有相同数量(和类型)列的表,然后执行操作! I recommend creating separate tables for each tab (if the number and type of columns are different). 我建议为每个选项卡创建单独的表(如果列的数量和类型不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM