I don’t know you, but people from other BU are old fashioned guys that do everything by hand, and then send you an Excel file to “put this data in the database” or “find info related to this codes”.
This is an hassle, especially if you don’t have the Microsoft.ACE.OLEDB provider installed in your machine (or for some reason SSMS doesn’t recognize it or you have the wrong version) and hence you cannot use the Import Data task (which is garbage, anyway).
A quick solution that I used in the past is saving the xlsx file as CSV and then use the flat file import wizard to quickly import the data into my staging DB; Still, some characters may prove problematic in a CSV file and you have to choose the right separator, when you have an Excel file with more than one worksheet you have to save more than one CSV file and don’t get me started in how the Flat File Import Wizard analyzes only the first rows of the file and hence chooses datatypes and nullable columns in a non-reliable way (aka I cannot press Advance->Advance->Advance->Done without looking, unaccepptable)
Here is the code
Since I’m lazy, I scripted out a Powershell file that does the trick automatically for me, here it is for you:
$File = "C:\Users\emeazzo\WhereverIKeepMyFiles\MyFile.xlsx" $Instance = "YourSQLInstance" $Database = "TheDBWhereYouPutThisStuff" $fileName = [System.IO.Path]::GetFileNameWithoutExtension($File) foreach($sheet in Get-ExcelSheetInfo $File) { $data = Import-Excel -Path $File -WorksheetName $sheet.name | ConvertTo-DbaDataTable $tablename = $fileName + '-' + $sheet.Name Write-DbaDataTable -SqlInstance $Instance -Database $Database -InputObject $data -AutoCreateTable -Table $tablename }
In my case, it loads the data in the Database that I’ve specified using the Filename as the first part of the Table name and it concatenates the worksheet name as second part of the table name, nothing fancy.
Modules Info
As you can see, I’m using the dbatools and ImportExcel PS Modules, which are free, open source and you can install them like this:
Install-Module -Name ImportExcel Install-Module -Name dbatools #Or, If you don't have admin rights or don't want to install these for all users Install-Module -Name ImportExcel -Scope CurrentUser Install-Module -Name dbatools -Scope CurrentUser
The ImportExcel module is pretty neat for working with excel files in a programmatical way, without having to use COM Objects and all the usual mess. DBATools is a massive repository of scripts for all kind of SQL Server professionals, especially DBAs, but not limited to imho.
That’s it! You can implement this in whatever manual, automatic or semi-automatic file pipeline that you have, and enjoy not having to upload file manually.
Hi, Thanks for this tutorial. This really helped a lot.
Also, can you please give a tutorial on how to import Multiple Excels along with Multiple Worksheets in Azure Blob Storage to Sql Server.
Hey Pramod,
You just need to iterate the process above over each file in your Azure Blob storage.
You can do that by either mounting your blob storage as a network drive and using get-childitems or access the files directly in the cloud with Azure CLI (https://docs.microsoft.com/en-us/powershell/module/azure.storage/get-azurestorageblobcontent?view=AZURERMPS-6.13.0)
Hi, can you show please let me know what parameter to tell that the files does not include column headers? How to tell it there is no column headers in the file, thank you.
Hello,
the Import-excel module has a -NoHeader option
You can refer to the module Git for more details https://github.com/dfinke/ImportExcel ๐
Before trying this, which seems simple enough, I see nothing in the code to authenticate you to the sql server. Where is that ?
By default dbatools (and the sqlserver powershell module too) use windows authentication, so I’m automatically logging in using the AD account I’ve opened the terminal with.
Alternatively, you can pass explicit credentials, refer to the dbatools docs: https://docs.dbatools.io/Write-DbaDbTableData
Hi, I have found the script works but if I run it again it appends the records, can you explain how I would clear down the table before importing it again in the script?
Simply add the -Truncate tag to Write-DbaDataTable
Check the DBATools docs https://docs.dbatools.io/Write-DbaDbTableData
Great Post, very helpful..
Maybe I’m late at the party, but it’s a very interesting approach, thank you for sharing! Beside that, your blog is quite interesting, and your replies to the comments as well!
Good work, thank you.
Grazie Raffaele ๐
Echoing thanks for this tutorial. I’ve been doing this through a dtsx job in VS, but I think this will be more efficient doing it this way if you can help me with a question – I’m wondering how to import the contents of a single sheet in the workbook. Is there a way to do that? Thanks again, and I’m looking forward to your response.
Of course, instead of looping through all of the sheets just pick the one you need and run the command ๐
Thanks for the response and the tutorial.