简体   繁体   English

如何使用php将Excel数据导入到现有的mysql表中

[英]how do I import an excel data into an existing mysql table with php

I am working on a project, where the users can create a new contact group as well as be able to update existing ones with excel data. 我正在做一个项目,用户可以在其中创建一个新的联系人组,也可以使用excel数据更新现有的联系人组。

creating a new contact group is not the problem, but inserting the content of the document into the database has been a major problem for me. 创建新的联系人组不是问题,但是将文档内容插入数据库对我来说是一个主要问题。

or which format can be imported easily into the database with php. 或可以使用php轻松将哪种格式导入数据库。

The data looks like this: 数据如下所示:

|name | |名称| |email | |电子邮件| |telephone| |电话|

|me | |我| |we@us.com| | we@us.com | |080234444| | 080234444 | etc 等等

any advice, suggestion or direction will be appreciated. 任何建议,建议或指示将不胜感激。

MySql can easily import CSV* data from a file using the LOAD DATA INFILE command. MySql可以使用LOAD DATA INFILE命令轻松地从文件导入CSV *数据。 If you need to parse other excel data, you will need extra methods and logic to read the file and manually populate the database. 如果需要解析其他excel数据,则需要其他方法和逻辑来读取文件并手动填充数据库。

You can read more info on LOAD DATA INFILE 您可以阅读有关LOAD DATA INFILE的更多信息

*Essentially any delimited text (TSV etc) *基本上是任何带分隔符的文本(TSV等)

Please note there are many limitations/gothchas with delimited data, for example, the inability to hold unescaped commas in csv text. 请注意,使用分隔的数据有很多限制/难题,例如,无法在csv文本中保留未转义的逗号。 Be sure you source file adheres and respects CSV rules or else you may end up with corrupt data. 确保您的源文件遵守并遵守CSV规则,否则可能会导致数据损坏。

EDIT 编辑

Now that you have provided some example of your data you may want to try this (or something similar): 现在,您已经提供了一些数据示例,您可能想尝试一下(或类似方法):

LOAD DATA INFILE `your filename` INTO `Table` 
Fields Terminated BY '|' 
Lines Terminated BY '\r\n'
IGNORE 1 LINES 
(@Col1, @Col2, @Col3, @Col4, @Col5)
SET Name = TRIM(@Col1), Email = TRIM(@Col3), Telephone = TRIM(@Col5)

Here, you load the data into temporary variables, which then get instered into the table. 在这里,您将数据加载到临时变量中,然后将其插入表中。 Note the file must exist on the local machine. 请注意该文件必须存在于本地计算机上。

EDIT 编辑

Try this fixed SQL 试试这个固定的SQL

LOAD DATA INFILE 'your filename' INTO TABLE `Table` 
Fields Terminated BY '|' 
Lines Terminated BY '\r\n'
IGNORE 1 LINES 
(@Col1, @Col2, @Col3, @Col4, @Col5)
SET Name = TRIM(@Col1), Email = TRIM(@Col3), Telephone = TRIM(@Col5)

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

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