Click here to Skip to main content
Licence CPOL
First Posted 3 Sep 2008
Views 70,110
Bookmarked 55 times

Import Excel files to SQL Server dynamically

By | 3 Sep 2008 | Article
This article shows how to import Excel files to SQL Server dynamically.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This article will show you how to import an Excel file to SQL Server, dynamically, without the need to open the Import wizard from SQL Server. This could be of great help if you have a lot of Excel files to import.

Using the code

First, create the Datamanage class that holds the import code. The “excelCommand” will create a new table in the SQL Server database, taking into consideration the names and data types of the Excel file fields.

Public Class Datamanage
    ''' <summary />
    ''' Imports the selected Excel file to SQL Server 
    ''' </summary />
   Public Sub importToServer(ByVal ExcelPath As String, _
              ByVal ServerName As String, _
              ByVal DBName As String, ByVal UserName As String, _
              ByVal Password As String, ByVal InsertedTableName As String)
        Try
            Dim ExceCon As String = _
                &quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=&quot; & _
                ExcelPath & &quot;; Extended Properties=Excel 8.0&quot;
            Dim excelConnection As System.Data.OleDb.OleDbConnection = _
                New System.Data.OleDb.OleDbConnection(ExceCon)
            excelConnection.Open()
            'Dim OleStr As String = &quot;SELECT * INTO [ODBC; Driver={SQL Server};&quot; & _
                 &quot;Server=Hoss;Database=Excel_Test;Uid=sa;Pwd=sa2008; ].[myTable]   
            FROM [Sheet1$];&quot;
            Dim OleStr As String = &quot;SELECT * INTO [ODBC; Driver={SQL Server};Server=&quot; _
                & ServerName & &quot;;Database=&quot; & DBName & &quot;;Uid=&quot; & _
                UserName & &quot;;Pwd=&quot; & Password & &quot;; ].[&quot; & _
                InsertedTableName & &quot;]   FROM [Sheet1$];&quot;
            Dim excelCommand As New System.Data.OleDb.OleDbCommand(OleStr, _
                                                      excelConnection)
            excelCommand.ExecuteNonQuery()
            excelConnection.Close()
        Catch ex As Exception
            Throw New Exception(&quot;Error: &quot; & ex.Message)
        End Try
    End Sub
End Class

Now, the main form:

Imports System.IO
Public Class ImportExcel
    Dim dm As New Datamanage
    Private Sub Button2_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button2.Click
        OpenFileDialog1.ShowDialog()
        txtexcelPath.Text = OpenFileDialog1.FileName
                     
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click
        Try
                Dim ExcelPath As String = txtexcelPath.Text.ToLower()
                Dim Ext As String = _
                    ExcelPath.Substring(ExcelPath.LastIndexOf(&quot;.&quot;) + 1)
                If File.Exists(ExcelPath) AndAlso (Ext.Equals(&quot;xls&quot;) _
                        Or Ext.Equals(&quot;xlsx&quot;)) Then
                    dm.importToServer(OpenFileDialog1.FileName, txtServer.Text,_
                        txtDbName.Text, txtusername.Text, txtpassword.Text,_
                        txtInsertedTableName.Text)
                    ProgressBar1.Visible = False
                    MessageBox.Show(&quot;File imported successfully&quot;)
                 End If
        Catch ex As Exception
            ProgressBar1.Visible = False
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Import_Excel.jpg

I hope this helps!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohammad Al Hoss

Software Developer
CME Offshore
Lebanon Lebanon

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNeed to Import Excel csv File Pinmemberimahaboob5:42 14 May '12  
AnswerTo Mohammed - Importing CSV files to SQL Server in C# [modified] PinmemberShashidhar Jarung4:27 9 Apr '12  
GeneralMy vote of 3 Pinmemberwaaqee19:18 25 Jul '11  
GeneralImport Excel files to SQL Server dynamically PinmemberBhavik170518:35 11 Aug '10  
GeneralRegarding source code PinmemberMember 320625018:59 16 Nov '09  
QuestionODBC Connection error PinmemberNisu Cheriyan23:53 30 Mar '09  
GeneralCould not find installable ISAM PinmemberKaran Mathur5:46 29 Mar '09  
GeneralNeed help : Error when executing application Pinmemberbondan_intikom20:05 21 Oct '08  
GeneralRe: Need help : Error when executing application PinmemberMohammad Al Hoss21:21 21 Oct '08  
GeneralRe: Need help : Error when executing application Pinmemberbondan_intikom21:46 21 Oct '08  
GeneralRe: Need help : Error when executing application PinmemberMohammad Al Hoss10:27 22 Oct '08  
GeneralRe: Need help : Error when executing application Pinmemberbondan_intikom15:30 22 Oct '08  
GeneralRe: Need help : Error when executing application PinmemberMohammad Al Hoss4:15 23 Oct '08  
QuestionRe: Need help : Error when executing application Pinmemberbondan_intikom19:29 3 Nov '08  
AnswerRe: Need help : Error when executing application PinmemberMohammad Al Hoss21:37 6 Nov '08  
QuestionTo Mohammad Pinmemberjilynii0:44 11 Sep '08  
AnswerRe: To Mohammad PinmemberMohammad Al Hoss1:58 12 Sep '08  
QuestionRe: To Mohammad [modified] Pinmemberjotapecardas22:39 19 Jul '10  
AnswerRe: To Mohammad [adding code solution] Pinmemberjotapecardas3:29 20 Jul '10  
AnswerRe: To Mohammad PinmemberMohammad Al Hoss21:19 20 Jul '10  
GeneralNeed Help. Pinmemberashu fouzdar2:50 4 Sep '08  
GeneralStrings not imported correctly PinmemberParas Shah7:57 3 Sep '08  
GeneralRe: Strings not imported correctly PinmemberMohammad Al Hoss22:21 3 Sep '08  
GeneralRe: Strings not imported correctly Pinmemberjabit5:58 26 May '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by Mohammad Al Hoss
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid