将数据从 Excel 移动到 SQL Server 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16061019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving data from Excel to SQL Server table
提问by jth41
I have a very Simple excel sheet:
我有一个非常简单的excel表:
I am wanting to put this data into a table in SQL Server. I also wanted to add a field that contains a date.
我想将此数据放入 SQL Server 中的表中。我还想添加一个包含日期的字段。
what is the best way to do this?
做这个的最好方式是什么?
回答by Travis
Create a table in SQL server with the same number of fields that you have in the spreadsheet.
在 SQL Server 中创建一个表,其字段数与电子表格中的字段数相同。
In SQL Server Management Studio Object Explorer you can right click the table you created and select "Edit top 200 rows". Highlight the data you want to copy in Excel and right click -> copy. Right click the space to the left of the first row in the SSMS edit tabe widow and paste.
在 SQL Server Management Studio 对象资源管理器中,您可以右键单击您创建的表并选择“编辑前 200 行”。在 Excel 中突出显示要复制的数据,然后右键单击 -> 复制。右键单击 SSMS 编辑选项卡中第一行左侧的空间并粘贴。
After the import check the SQL table to make sure it has the same amount of rows as the spreadsheet.
导入后检查 SQL 表以确保它具有与电子表格相同的行数。
You can add the date column to the SQL table after you do the data import. Or add it in the spreadsheet before you do the import.
执行数据导入后,您可以将日期列添加到 SQL 表中。或者在导入之前将其添加到电子表格中。
回答by OliC
You can first create the table in SQL Server with the added field and then use the import wizard in the Mangement Studio for importing the excel file. Or you create the table during the import task, and you add the new field later.
您可以先在 SQL Server 中使用添加的字段创建表,然后使用 Mangement Studio 中的导入向导导入 excel 文件。或者您在导入任务期间创建表,稍后添加新字段。
回答by granadaCoder
Option 1:
选项1:
Read the data in an IDataReader, and then call a stored procedure to insert the data.
在 IDataReader 中读取数据,然后调用存储过程插入数据。
I use the above when I have ~~alot~~ of rows to import and I want to validate them outside of the database.
当我有~~很多~~行要导入并且我想在数据库之外验证它们时,我会使用上面的内容。
Option 2:
选项 2:
http://support.microsoft.com/kb/321686
http://support.microsoft.com/kb/321686
or search for:
或搜索:
Select FROM OPENDATASOURCE Excel
Option N:
选项 N:
There are other options out there.
还有其他选择。
It depends what you like, how much time you want to put into it, is it a "one off" or you gotta do it for 333 files every day.
这取决于你喜欢什么,你想投入多少时间,是“一次性”还是你每天必须做 333 个文件。
回答by Mike
回答by Bond_007
It can be also be done by creating a batch file.
也可以通过创建批处理文件来完成。
For this you already need to have a table created on server with the same data structure as you have in excel.
为此,您已经需要在服务器上创建一个与 Excel 中数据结构相同的表。
Now using BCP you can import that data from the excel file and insert it into sql table.
现在使用 BCP,您可以从 excel 文件中导入该数据并将其插入到 sql 表中。
BCP utility function
BCP 效用函数
sqlcmd -S IP -d databasename -U username -P passwd -Q "SQL query mostly it should be truncate query to truncate the created table"
bcp databasename.dbo.tablename in "Excel file path from where you need to input the data" -c -t, -S Server_IP -U Username -P passwd -b provide batch size
You can refer to the link below for more options on the bcp utility:
您可以参考以下链接,了解有关 bcp 实用程序的更多选项:
回答by K_B
Open your SQL server interface software, add the date field to the table.
打开您的 SQL 服务器接口软件,将日期字段添加到表中。
Go to excel, add the date column, copy the excel data.
转到excel,添加日期列,复制excel数据。
Go to your SQL server interface software and use the functionality to import the data from the clipboard. (SQL server interface software that has this is for example Database4.net, but if you have another package with the functionality then use that.)
转到您的 SQL 服务器接口软件并使用该功能从剪贴板导入数据。(具有此功能的 SQL 服务器接口软件是例如 Database4.net,但如果您有另一个具有该功能的包,则使用它。)
Alternatively use VBA with DOA or ADO to interact with the SQL server database and use SQL statements to add the field and copy the data into your table
或者使用带有 DOA 或 ADO 的 VBA 与 SQL 服务器数据库交互,并使用 SQL 语句添加字段并将数据复制到表中