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?
Comment
Watch Question

Commented:
The excel part

http://vb.net-informations.com/excel..._open_file.htm
http://vb.net-informations.com/excel..._worksheet.htm

The SQL-part
http://www.developerfusion.com/article/4278/using-adonet-with-sql-server/
on page 3 (article continues) you see an UPDATE as on how your insert should be written

Commented:
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.

Author

Commented:
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',
--'Text;Database=C:\;HDR=NO',
--'SELECT * FROM userlist.csv')
--go


this example helps you a lot  
Commented:
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]
        'GO
        'EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
        'GO
        'EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
        'GO

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

        ' 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 "

Author

Commented:
Solution guided me to the answer. Thanks.

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