how to import an excel file into sqlserver 2008

泪湿孤枕 提交于 2019-12-05 12:51:56

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

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

I think using OPENROWSET or OPENDATASOURCE will be the easiest way, without the wizard. (see Distributed Queries)

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.

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

Use ExcelReaderFactory to read excel

You can use the below code

VB.net Code

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

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.

or

create xml and send to database

or

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

Please follow the below article to implement it.

Read excel in SQL stored Procedure

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

on the next page select sql server as destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!