4

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

Thanks Prady

Prady
  • 10,978
  • 39
  • 124
  • 176

3 Answers3

6

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

Chris Diver
  • 19,362
  • 4
  • 47
  • 58
  • Correct, but it is the query portion of `OPENROWSET` look at the link I provided. – Chris Diver May 25 '11 at 10:59
  • 2
    If you are having problems with x64 machines, see this link for x64 Jet engine, https://www.microsoft.com/en-us/download/details.aspx?id=13255 – Neil May 04 '12 at 09:42
1

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

Pankaj
  • 9,749
  • 32
  • 139
  • 283
0

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

sara
  • 309
  • 1
  • 2
  • 9