Skip to content

Instantly share code, notes, and snippets.

@ctigeek ctigeek/Import-XlsData.ps1
Last active Mar 13, 2018

What would you like to do?
Query XLS spreadsheet from powershell
function Import-XlsData($filePath) {
## You have to have these drivers...
##The first row will be the column names
##This only looks at the first sheet. If you want a different sheet you'll have to adjust the table name retrieved from $schema.
$strProvider = "Provider=Microsoft.ACE.OLEDB.12.0"
$strDataSource = "Data Source=$filePath"
$strExtend = "Extended Properties='Excel 12.0;IMEX=1'"
$objConn = New-Object System.Data.OleDb.OleDbConnection("$strProvider;$strDataSource;$strExtend")
$schema = $objConn.GetOleDbSchemaTable([System.Data.OleDb.OleDbSchemaGuid]::Tables, $null)
$table = $schema.Rows[0].TABLE_NAME
$dataTable = New-Object System.Data.DataTable
$command = $objConn.CreateCommand()
$command.CommandText = "select * from [$table]"
$dataAdapter = New-Object -TypeName System.Data.OleDb.OleDbDataAdapter -ArgumentList $command
$dataAdapter.Fill($dataTable) | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.