-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathInvoke-ExcelQuery.ps1
63 lines (52 loc) · 2.54 KB
/
Invoke-ExcelQuery.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#Requires -Version 5
function Invoke-ExcelQuery {
<#
.SYNOPSIS
Helper method for executing Read-OleDbData with some basic defaults.
For additional help, see documentation for Read-OleDbData cmdlet.
.DESCRIPTION
Uses Read-OleDbData to execute a sql statement against a xlsx file. For finer grained control over the interaction, you may use that cmdlet. This cmdlet assumes a file path will be passed in and the connection string will be built with no headers and treating all results as text.
Running this command is equivalent to running the following:
$FullName = (Get-ChildItem $Path).FullName
Read-OleDbData `
-ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" `
-SqlStatement $Query
Note that this command uses the MICROSOFT.ACE.OLEDB provider and will not work without it.
If needed, please download the appropriate package from https://www.microsoft.com/en-us/download/details.aspx?id=54920.
.EXAMPLE
Invoke-ExcelQuery .\test.xlsx 'select ROUND(F1) as [A1] from [sheet3$A1:A1]'
.EXAMPLE
$Path = (Get-ChildItem 'test.xlsx').FullName
$Query = "select ROUND(F1) as [A] from [sheet1$A1:A1]"
Read-XlsxUsingOleDb -Path $Path -Query $Query
.EXAMPLE
$ReadDataArgs = @{
Path = .\test.xlsx
Query = Get-Content query.sql -Raw
}
$Results = Invoke-ExcelQuery @ReadDataArgs
#>
param(
#The path to the file to open.
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $Path, # var name consistent with Import-Excel
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $Query # var name consistent with Invoke-Sqlcmd
)
try {
if ((New-Object system.data.oledb.oledbenumerator).GetElements().SOURCES_NAME -notcontains "Microsoft.ACE.OLEDB.12.0") {
Write-Error "Microsoft.ACE.OLEDB.12.0 provider is missing! Please install from https://www.microsoft.com/en-us/download/details.aspx?id=54920"
return
}
}
catch {
Write-Error "System.Data.OleDb is not working or you are on an unsupported platform."
return
}
$FullName = (Get-ChildItem $Path).FullName
Read-OleDbData `
-ConnectionString "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$FullName;Extended Properties='Excel 12.0 Xml;HDR=NO;IMEX=1;'" `
-SqlStatement $Query
}