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


Insert large amount of Excel data into a MySQL database

我的excel表格中有大量的数据。excel工作表包含5 ~ 6个选项卡,不同选项卡中的列不同。现在我想在数据库中插入所有的数据使用数组,但问题是由于没有列。是否有任何方法将列名作为数组键名?

下面是我的代码:
$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);

如果数据量大,我更喜欢将excel数据导出(另存为)CSV(或TSV)文件,然后导入到DB。

假设导出的文件名为'data.csv'。您可以在mysql命令解释器中使用此命令导入文件。

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

确保在excel中创建一个具有相同列数(和类型)的表,然后启动!我建议为每个选项卡创建单独的表(如果列的数量和类型不同)。