从Excel电子表格输入数据到数据库?

3
我正在寻找一种巧妙的方法,从Excel电子表格中提取500多行数据并将其输入到我的数据库中。
该电子表格类似于 this 我的'tbl_foot_teams'表如下所示:
id | name | rating
简单地说,我需要将电子表格中的两列输入到数据库字段名称和评级中。
有没有有效的方法来实现这一点?一个个输入将花费我荒谬的时间!
谢谢
6个回答

2

将Excel文件另存为CSV格式,并使用LOAD DATA INFILE命令导入数据。

您的Excel文件中没有id字段。请在表中创建id字段并设置为自动增量,然后使用以下命令 -

LOAD DATA INFILE 'file_name.csv' INTO TABLE tbl_foot_teams
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
-- IGNORE 1 LINES -- if csv file has column headers
(name, rating)
SET id = NULL; -- this will set unique value for each row

此外,还要查看GUI数据导入工具(Excel或CSV格式),位于dbForge Studio for MySQL中。

该文件应由MySQL服务器访问。您可以指定完整路径,例如- LOAD DATA INFILE 'c:/folder1/file_name.csv'。 - Devart
这在phpmyadmin中给了我一个错误,但没有指明是什么!我认为这是文件地址的问题...它在Mac的下载部分。有什么想法吗? - sark9012
现在出现以下错误:#1045 - 用户'dbo378105509'@'%'被拒绝访问(使用密码:是) - sark9012
拒绝访问 - 请检查密码。 - Devart
密码检查在哪里?只有在这一个查询中吗? - sark9012
显示剩余2条评论

0

您没有指定使用的数据库,但是使用MySQL实现此目的的简单方法是将电子表格导出为csv文件,然后使用mysqlimport导入到MySQL中。

用户Philippe Jausions在this MySQL page的评论中描述了这一过程:

If you are one of the many people trying to import a CSV file into MySQL using mysqlimport under MS-Windows command/DOS prompt, try the following:

mysqlimport --fields-optionally-enclosed-by=""" --fields-terminated-by=, --lines-terminated-by="\r\n" --user=YOUR_USERNAME --password YOUR_DATABASE YOUR_TABLE.csv

Between quotes " and backslashes \ it can really give you a hard time finding the proper combination under Windows...

I usually run this command from the folder containing the YOUR_TABLE.csv file.

If you have a header in your .csv file with the name of columns or other "junk" in it, just add a --ignore-lines=X to skip the first X lines (i.e. --ignore-lines=1 to skip 1 line)

If your fields are (optionally) enclosed by double-quotes " and which themselves are doubled inside a value (i.e. a double double-quote "" = 1 double-quote ") then also use --fields-escaped-by=\ (default) and NOT --fields-escaped-by="""


0
在phpmyadmin中,您有一个从Excel导入的选项。
如果没有,则可能有从CSV导入,因此只需将电子表格转换为CSV即可。
如果您没有上述任何一种,则可以编写一个PHP函数,打开文本文件,按行拆分,然后按值拆分。

0
如果我们谈论50行,您可以轻松地在电子表格上创建一个新列,并使用公式将您的值连接到插入语句中。类似于以下内容:
=concat("insert into tbl_foot_teams ( name , rating) values ( ", $b8, " ...)
然后,将计算公式文本结果复制并粘贴到您的数据库中。

0

将文件转换为CSV格式后,您可以将其导入到支持从CSV文件导入的任何数据库中(如MySQL、PostgreSQL等),但您需要通过命令行执行以下操作:

您可以使用编程语言,例如Python,并使用其库,例如Excel电子表格读取库,然后再使用与SQL数据库进行交互的库

您连接到数据,加载Excel文件,并循环遍历每一行并提取您想要的任何列数据。然后,您将该数据执行INSERT语句。

如果您在服务器上安装了phpMyAdmin,则可以使用从CSV导入选项,但您首先必须将Excel电子表格另存为CSV(逗号分隔值)文件。


0

从Excel端开始工作,您可以使用ADO,例如Excel VBA:写入mysql数据库

Dim cn As ADODB.Connection

''Not the best way to get the name, just convenient for notes
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")

''For this to work, you must create a DSN and use the name in place of 
''DSNName, however, you can also use the full connection string
strSQL = "INSERT INTO [ODBC;DSN=DSNName;].NameOfMySQLTable " _
& "Select AnyField As NameOfMySQLField FROM [Sheet1$];"

cn.Execute strSQL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接