SQL 如何将excel文件导入sqlserver 2008

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6123113/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:45:08  来源:igfitidea点击:

how to import an excel file into sqlserver 2008

sqlsql-server-2008sql-server-2008r2-express

提问by Prady

How can i import an excel file into a new table in sqlserver2008 express edition using an sql query without using the import wizard

如何在不使用导入向导的情况下使用 sql 查询将 excel 文件导入到 sqlserver2008 快速版中的新表中

Thanks Prady

谢谢普拉迪

回答by Chris Diver

There is a microsoft knowledge base article that lays out all the ways this is possible.

有一篇微软知识库文章列出了所有可能的方法。

http://support.microsoft.com/kb/321686

http://support.microsoft.com/kb/321686

I think using OPENROWSETor OPENDATASOURCEwill be the easiest way, without the wizard. (see Distributed Queries)

我认为使用OPENROWSETorOPENDATASOURCE将是最简单的方法,无需向导。(见分布式查询)

SELECT * INTO XLImport4 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test\xltest.xls', [Customers$])

See OPENROWSET documentation, with examples lower down the page.

请参阅 OPENROWSET 文档,以及页面下方的示例。

http://msdn.microsoft.com/en-us/library/ms190312.aspx

http://msdn.microsoft.com/en-us/library/ms190312.aspx

回答by Pankaj

Use ExcelReaderFactoryto read excel

使用ExcelReaderFactory来读取Excel

You can use the below code

您可以使用以下代码

VB.net Code

VB.net 代码

Dim stream As FileStream = File.Open("YouExcelFilePath.xls", FileMode.Open, FileAccess.Read)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream)
Dim result As DataSet = excelReader.AsDataSet()
excelReader.Close()
result.Dispose()

C# Code

C# 代码

FileStream stream = File.Open("YouExcelFilePath.xls", FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
DataSet result = excelReader.AsDataSet();
excelReader.Close();
result.Dispose();

Now use can do bulk import using Bulkcopy class.

现在可以使用 Bulkcopy 类进行批量导入。

or

或者

create xml and send to database

创建xml并发送到数据库

or

或者

Use OPENROWSETto read the excel file in Stored Procedure and insert/update the data.

使用OPENROWSET读取存储过程并插入Excel文件/更新数据。

Please follow the below article to implement it.

请按照下面的文章来实现它。

Read excel in SQL stored Procedure

在SQL存储过程中读取excel

回答by sara

right click on the database name/go to task and then select import data

右键单击数据库名称/转到任务,然后选择导入数据

as a source select an excel file that you created before and choose it's path

作为源选择您之前创建的 excel 文件并选择它的路径

on the next page select sql server as destination

在下一页选择 sql server 作为目标