Click here to Skip to main content
15,880,608 members
Articles / Database Development / SQL Server
Article

Import Excel files to SQL Server dynamically

Rate me:
Please Sign up or sign in to vote.
4.71/5 (18 votes)
3 Sep 2008CPOL 185.5K   61   33
This article shows how to import Excel files to SQL Server dynamically.

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.

VB
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:

VB
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)


Written By
Software Developer CME Offshore
Lebanon Lebanon
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: To Mohammad Pin
Mohammad Al Hoss12-Sep-08 1:58
Mohammad Al Hoss12-Sep-08 1:58 
QuestionRe: To Mohammad [modified] Pin
jotapecardas19-Jul-10 22:39
jotapecardas19-Jul-10 22:39 
AnswerRe: To Mohammad [adding code solution] Pin
jotapecardas20-Jul-10 3:29
jotapecardas20-Jul-10 3:29 
AnswerRe: To Mohammad Pin
Mohammad Al Hoss20-Jul-10 21:19
Mohammad Al Hoss20-Jul-10 21:19 
GeneralNeed Help. Pin
Ashutosh Phoujdar4-Sep-08 2:50
Ashutosh Phoujdar4-Sep-08 2:50 
GeneralStrings not imported correctly Pin
Paras Shah3-Sep-08 7:57
Paras Shah3-Sep-08 7:57 
GeneralRe: Strings not imported correctly Pin
Mohammad Al Hoss3-Sep-08 22:21
Mohammad Al Hoss3-Sep-08 22:21 
GeneralRe: Strings not imported correctly Pin
jabit26-May-10 5:58
jabit26-May-10 5:58 

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

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