We help IT Professionals succeed at work.

Import Excel sheet into SQL table

I'm trying to create a simple VB.Net program that imports an Excel sheet into a SQL server database table.
As the format of the excel file is fixed and can not be altered it can not be a straight import.
The import needs to start at line 13 and I need to import selected columns into the correct column in SQL server.
Im hoping to do this in code and not through SSIS, but will bow down if this really is the best and quickest way.
Any idea or examples please?
Watch Question

The excel part


The SQL-part
on page 3 (article continues) you see an UPDATE as on how your insert should be written

Import the data into an intermediate or temporary table and filter the data according to your need. You can add another column which gives you the line number in the excel sheet.


I will try this solution but I'm trying to not use a reference to the excel objects as this could tie me into a particular version of excel. Is there a way to use an excel connection string then export the data directly to a SQL table using a SQL command?
INSERT INTO [DBName].[dbo].[TabelNAme]
--           ([column1]
--           ,[column 3]
--           ,[column 4]
--           ,[column 5]
--           ,[column 6]
--           ,[column 7]
--           ,[column 8)
--     SELECT 'E0656BF5-52DD-4D6B-A85A-733561EABF65',f2,f3,f4,f5,f6,f7
--FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
--'SELECT * FROM userlist.csv')

this example helps you a lot  
This solution is about to work for me, guided inthe right direction by the comment above.
I needed to remove the top (13) header lines so added the "where Not f2 is null"
The hard part seems to be getting the server setup to not generate errors.

        ' ******** May need this
        'USE [master]
        'EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
        'EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1

        ' ******** May also need this
        'sp_configure() 'show advanced options', 1;
        'sp_configure() 'Ad Hoc Distributed Queries', 1;

        ' This did not work
        'select top 100 *
        'from OPENROWSET('Microsoft.ACE.OLEDB.12.0','Database=D:\Development\MarketData\Source\PeopleSoftConvert\Copy.xlsx;',
        ''select * from [h8cw04hr_1297668]')

        Dim sSQL As String
        sSQL = "SELECT top 10 * " & _
                "FROM OPENROWSET('MSDASQL','DRIVER=Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb);DBQ=D:\Development\Copy.xlsx', 'SELECT * FROM [sheet1$]') " & _
                "where Not f2 Is null "


Solution guided me to the answer. Thanks.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.