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

问题描述:

我正在寻找一种巧妙的方式,从excel电子表格中提取500行数据并输入到我的数据库中。从excel电子表格中输入数据到数据库中?

电子表格就像是this

我的表 'tbl_foot_teams' 载列

ID |名称|评分

很简单,我需要输入从电子表格中获取两列到数据库字段的名称和评级。

是否有任何有效的方法来实现这一目标? 个别地,它会花我一个荒谬的时间!

谢谢

保存Excel文件爆炸为CSV并使用LOAD DATA INFILE命令导入数据。

您的excel文件没有id字段。让id场在表中AUTO_INCREMENT,并用命令这样的 -

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 

此外,在dbForge Studio for MySQL看看GUI Data Import tool(Excel或CSV格式)。

+0

我在哪里都将文件放在这个工作? – sark9012

+0

这个文件应该被MySQL服务器访问。您可以指定完整路径,例如 - LOAD DATA INFILE'c:/folder1/file_name.csv' – Devart

+0

这给了我一个phpmyadmin错误,但没有指定它是什么!我认为这是一个文件地址的问题......它在mac上的下载部分。有任何想法吗? – sark9012

在phpmyadmin中,您有一个从Excel导入选项。
如果您还没有,可以从CSV导入,所以只需将电子表格转换为CSV。
如果你有上面这些都不是,你可以写打开一个文本文件中的PHP函数,使得按行爆炸,然后由值

如果我们谈论的是50行,您可以在电子表格中轻松创建一个新列,并使用公式将您的值连接到插入语句。喜欢的东西:

= CONCAT( “插入tbl_foot_teams(名称,等级)值(” $ B8,“......

然后,复制粘贴计算公式文本结果数据库上

。你没有指定你正在使用的数据库,但是用MySQL做一个简单的方法是将电子表格导出为csv文件,然后用mysqlimport导入到MySQL中。 this MySQL page,来自Philippe Jausions用户:

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="""

转换为CSV文件后,您可以导入任何支持从CSV文件(MySQL,PostgreSQL等)导入的数据库),但你必须在命令行里这么做:

可以使用一种编程语言,如Python和使用一个库,例如Excel spreadsheet reading library,然后是另一个library for interfacing with your SQL database

您连接到数据,加载Excel文件,并遍历每一行并提取您想要的任何列数据。然后你拿这些数据并执行INSERT声明。

如果您在服务器上安装了phpMyAdmin,则可以使用the Import from CSV option,但您首先必须将Excel电子表格重新保存为CSV(逗号分隔值)文件。

从Excel中结束时,您可以使用ADO,例如Excel VBA: writing to mysql database

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