Chad's profileChad Miller's BlogBlogListsNetworkMore Tools Help

Blog


    May 12

    Using Powershell to Import Excel file into SQL Server

    This is a quick script which demonstrates how easy it is to import an Excel file into a SQL Server table using Powershell. The script is posted on PoshCode also:
     
    #Change these settings as needed
    $filepath = 'G:\PowershellvTSQL\backupset.xlsx'
    #Comment/Uncomment connection string based on version
    #Connection String for Excel 2007:
    $connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$filepath`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
    #Connection String for Excel 2003:
    #$connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=`"$filepath`";Extended Properties=`"Excel 8.0;HDR=Yes;IMEX=1`";"
    $qry = 'select * from [backupset$]'
    $sqlserver = 'Z002\SQLEXPRESS'
    $dbname = 'SQLPSX'
    #Create a table in destination database with the with referenced columns and table name.
    $tblname = 'ExcelData_fill'
     
    #######################
    function Get-ExcelData
    {
     
        param($connString, $qry='select * from [sheet1$]')
     
        $conn = new-object System.Data.OleDb.OleDbConnection($connString)
        $conn.open()
        $cmd = new-object System.Data.OleDb.OleDbCommand($qry,$conn)
        $da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
        $dt = new-object System.Data.dataTable
        [void]$da.fill($dt)
        $conn.close()
        $dt
     
    } #Get-ExcelData
     
    #######################
    function Write-DataTableToDatabase
    {
        param($dt,$destServer,$destDb,$destTbl)
     
        $connectionString = "Data Source=$destServer;Integrated Security=true;Initial Catalog=$destdb;"
        $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
        $bulkCopy.DestinationTableName = "$destTbl"
        $bulkCopy.WriteToServer($dt)
     
    }# Write-DataTableToDatabase
     
    #######################
    $dt = Get-ExcelData $connString $qry
    Write-DataTableToDatabase $dt $sqlserver $dbname $tblname

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!412.trak
    Weblogs that reference this entry
    • None