将 Excel 电子表格列导入 SQL Server 数据库

发布于 2024-07-11 01:32:18 字数 97 浏览 7 评论 0原文

我有一个 Excel 电子表格,我想将选定的列导入到我的 SQL Server 2008 数据库表中。 该向导没有提供该选项。

是否存在任何简单的代码选项?

I have an Excel spreadsheet that I want to import select columns into my SQL Server 2008 database table. The wizard didn't offer that option.

Do any easy code options exist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

我不在是我 2024-07-18 01:32:18

连接到 Sql Server 2005 数据库后,从对象资源管理器窗口中右键单击要将表导入到的数据库。 选择任务-> 导入数据。
这是一个简单的工具,允许您将传入数据“映射”到适当的表中。
您可以保存脚本以便在需要时再次运行。

Once connected to Sql Server 2005 Database, From Object Explorer Window, right click on the database which you want to import table into. Select Tasks -> Import Data.
This is a simple tool and allows you to 'map' the incoming data into appropriate table.
You can save the scripts to run again when needed.

眼角的笑意。 2024-07-18 01:32:18

Microsoft 建议几种方法:

  • SQL Server 数据转换服务 (DTS)
  • Microsoft SQL Server 2005 集成服务 (SSIS) )
  • SQL Server 链接服务器
  • SQL Server 分布式查询
  • ActiveX 数据对象 (ADO) 和 Microsoft OLE DB Provider for SQL Server
  • ADO 和 Microsoft OLE DB Provider for Jet 4.0

如果向导 (DTS) 不起作用(我认为它应该)你可以尝试这样的东西 http://www.devasp.net/net/ articles/display/771.html 基本上建议做类似的事情

INSERT INTO [tblTemp] ([Column1], [Column2], [Column3], [Column4])

SELECT A.[Column1], A.[Column2], A.[Column3], A.[Column4]
FROM OPENROWSET 
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\Excel.xls;HDR=YES', 'select * from [Sheet1$]') AS A;

Microsoft suggest several methods:

  • SQL Server Data Transformation Services (DTS)
  • Microsoft SQL Server 2005 Integration Services (SSIS)
  • SQL Server linked servers
  • SQL Server distributed queries
  • ActiveX Data Objects (ADO) and the Microsoft OLE DB Provider for SQL Server
  • ADO and the Microsoft OLE DB Provider for Jet 4.0

If the wizard (DTS) isn't working (and I think it should) you could try something like this http://www.devasp.net/net/articles/display/771.html which basically suggests doing something like

INSERT INTO [tblTemp] ([Column1], [Column2], [Column3], [Column4])

SELECT A.[Column1], A.[Column2], A.[Column3], A.[Column4]
FROM OPENROWSET 
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\Excel.xls;HDR=YES', 'select * from [Sheet1$]') AS A;
谈场末日恋爱 2024-07-18 01:32:18

这听起来可能很漫长,但您可能想看看使用 Excel 生成 INSERT SQL 代码,您可以将其粘贴到查询分析器中以创建表。

如果由于 Excel 文件不在服务器上而无法使用向导,则效果很好

This may sound like the long way around, but you may want to look at using Excel to generate INSERT SQL code that you can past into Query Analyzer to create your table.

Works well if you cant use the wizards because the excel file isn't on the server

许一世地老天荒 2024-07-18 01:32:18

您可以使用 OPENROWSET,例如:

SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',  
    'Excel 8.0;IMEX=1;HDR=NO;DATABASE=C:\FILE.xls', '从 [Sheet1$] 中选择 *' 
  

只需确保该路径是服务器上的路径,而不是本地计算机上的路径。

You could use OPENROWSET, something like:

SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
  'Excel 8.0;IMEX=1;HDR=NO;DATABASE=C:\FILE.xls', 'Select * from [Sheet1$]'

Just make sure the path is a path on the server, not your local machine.

转身泪倾城 2024-07-18 01:32:18
go
sp_configure 'show advanced options',1  
reconfigure with override  
go  
sp_configure 'Ad Hoc Distributed Queries',1  
reconfigure with override  
go
SELECT * into temptable
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                'Excel 8.0;Database=C:\Documents and Settings\abhisharma\Desktop\exl\ImportExcel2SQLServer\ImportExcel2SQLServer\example.xls;IMEX=1',
                'SELECT * FROM [Sheet1$]')

select * from temptable
go
sp_configure 'show advanced options',1  
reconfigure with override  
go  
sp_configure 'Ad Hoc Distributed Queries',1  
reconfigure with override  
go
SELECT * into temptable
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                'Excel 8.0;Database=C:\Documents and Settings\abhisharma\Desktop\exl\ImportExcel2SQLServer\ImportExcel2SQLServer\example.xls;IMEX=1',
                'SELECT * FROM [Sheet1$]')

select * from temptable
烟─花易冷 2024-07-18 01:32:18

另一种选择是在 Excel 中使用 VBA,并编写宏来解析电子表格数据并将其写入 SQL。

一个例子如下: http://www.ozgrid.com/ forum/showthread.php?t=26621&page=1

Sub InsertARecord() 
Dim cnt As ADODB.Connection 
Dim rst As ADODB.Recordset 
Dim stCon As String, stSQL As String 
Set cnt = New ADODB.Connection 
Set rst = New ADODB.Recordset 

stCon = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=JOEY" 
cnt.ConnectionString = stCon 
stSQL = "INSERT INTO MyTable (Price)" 
stSQL = stSQL & "VALUES (500)" 

cnt.Open 
rst.Open stSQL, cnt, adOpenStatic, adLockReadOnly, adCmdText 

If CBool(rst.State And adStateOpen) = True Then rst.Close 
Set rst = Nothing 
If CBool(cnt.State And adStateOpen) = True Then cnt.Close 
Set cnt = Nothing 

End Sub

Another option is to use VBA in Excel, and write a macro to parse the spreadsheet data and write it into SQL.

One example is here: http://www.ozgrid.com/forum/showthread.php?t=26621&page=1

Sub InsertARecord() 
Dim cnt As ADODB.Connection 
Dim rst As ADODB.Recordset 
Dim stCon As String, stSQL As String 
Set cnt = New ADODB.Connection 
Set rst = New ADODB.Recordset 

stCon = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=JOEY" 
cnt.ConnectionString = stCon 
stSQL = "INSERT INTO MyTable (Price)" 
stSQL = stSQL & "VALUES (500)" 

cnt.Open 
rst.Open stSQL, cnt, adOpenStatic, adLockReadOnly, adCmdText 

If CBool(rst.State And adStateOpen) = True Then rst.Close 
Set rst = Nothing 
If CBool(cnt.State And adStateOpen) = True Then cnt.Close 
Set cnt = Nothing 

End Sub
一指流沙 2024-07-18 01:32:18

我假设您所说的“向导”指的是“SQL Server 导入和导出向导”。 (我也是个新手,所以我不明白大多数问题,更不用说大多数答案了,但我想我明白了)。 如果是这样,您不能获取电子表格或其副本,删除您不想导入的列,然后使用向导吗?

我总能找到用它来做我需要的事情的能力,而且我只使用 SQL Server 2000(不确定其他版本有何不同)。

编辑:事实上,我现在正在查看它,我似乎能够选择要将哪些列映射到现有表中的哪些行。 在“选择源表和视图”屏幕上,我检查正在使用的数据表,选择“目标”,然后单击“编辑...”按钮。 从那里您可以选择要映射到的 Excel 列和表列。

By 'the wiz' I'm assuming you're talking about the 'SQL Server Import and Export Wizard'. (I'm also pretty new so I don't understand most questions, much less most answers, but I think I get this one). If so couldn't you take the spreadsheet, or a copy of it, delete the columns you don't want imported and then use the wizard?

I've always found the ability to do what I need with it and I'm only on SQL Server 2000 (not sure how other versions differ).

Edit: In fact I'm looking at it now and I seem to be able to choose which columns I want to map to which rows in an existing table. On the 'Select Source Tables and Views' screen I check the datasheet I'm using, select the 'Destination' then click the 'Edit...' button. From there you can choose the Excel column and the table column to map it to.

萌吟 2024-07-18 01:32:18

如果您想要一个具有桌面界面(包括验证)的可视化工具..您可能会喜欢这个 Excel 工具。 您还可以使用该工具创建多用户数据编辑任务,甚至将数据从任何来源粘贴到 SQL Server。

如何验证 Excel 电子表格并将其导入 SQL Server 数据库:

http://leansoftware。 net/en-us/help/excel-database-tasks/worked-examples/how-to-import-excel-spreadsheet-to-sql-server-data.aspx

If you would like a visual tool with Desktop interface including validation .. you may well like this Excel tool. You can also use the tool to create multi user data-edit tasks, or even paste data to SQL server from any source..

How to Validate and Import Excel spreadsheet to SQL Server database:

http://leansoftware.net/en-us/help/excel-database-tasks/worked-examples/how-to-import-excel-spreadsheet-to-sql-server-data.aspx

紅太極 2024-07-18 01:32:18

Microsoft Access 是另一种选择。 您的计算机上本地可以有一个 Access 数据库,您可以将 Excel 电子表格导入其中(可用向导),并且 通过 ODBC 链接到 SQL Server 数据库表

然后,您可以在 Access 中设计一个查询,将 Excel 电子表格中的数据附加到 SQL Server 表。

Microsoft Access is another option. You could have a Access database locally on your machine that you import the excel spreadsheets into (wizards available) and link to the the SQL Server database tables via ODBC.

You could then design a query in access that appends data from the Excel spreadsheet to the SQL Server Tables.

祁梦 2024-07-18 01:32:18

我用过的最好的工具是 http://tools.perceptus.ca/ text-wiz.php?ops=7 你尝试过吗?

The best tool i've ever used is http://tools.perceptus.ca/text-wiz.php?ops=7 Did you try it?

南街女流氓 2024-07-18 01:32:18

导入向导确实提供了该选项。 您可以使用该选项编写自己的要导入数据的查询,也可以使用复制数据选项并使用“编辑映射”按钮忽略您不想导入的列。

The import wizard does offer that option. You can either use the option to write your own query for the data to import, or you can use the copy data option and use the "Edit Mappings" button to ignore columns you do not want to import.

第七度阳光i 2024-07-18 01:32:18

Excel + SQLCMD + Perl = exceltomssqlinsert

并且您可以使用 Excel 作为 MSSQL 数据库的前端...注意每个生成的sql插入文件开头的截断表...

Excel + SQLCMD + Perl = exceltomssqlinsert

and you can use your Excel as frond-end to MSSQL db ... Note the truncate table at the beginning of each generated sql insert file ...

不必了 2024-07-18 01:32:18

我使用过DTS(现在称为SQL Server导入和导出向导)。 我使用了这个教程< /a> 即使在 Sql 2008 和 excel 2010 (14.0) 中也对我很有用,

我希望这有帮助

-D

I have used DTS (now known as SQL server Import and Export Wizard). I used the this tutorial which worked great for me even in Sql 2008 and excel 2010 (14.0)

I hope this helps

-D

も星光 2024-07-18 01:32:18

首先,尝试 32 位版本的导入向导。 这显示了更多支持的导入格式。

背景:
一切都取决于您的 Office(运行时引擎)安装。

如果您没有安装 Office 2007 或更高版本,则导入向导(32 位)仅允许您导入 Excel 97-2003 (.xls) 文件。

如果您安装了 Office 2010 和 geater(也有 64 位版本,不推荐),则导入向导还支持 Excel 2007+ (.xlsx) 文件。

要了解运行时的概述,请参阅 “Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册

First of all, try the 32 Bit Version of the Import Wizard. This shows a lot more supported import formats.

Background:
All depends on your Office (Runtimes Engines) installation.

If you dont't have Office 2007 or greater installed, the Import Wizard (32 Bit) only allows you to import Excel 97-2003 (.xls) files.

If you have the Office 2010 and geater (comes also in 64 Bit, not recommended) installed, the Import Wizard also supports Excel 2007+ (.xlsx) files.

To get an overview on the runtimes see 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文