Connect Workday to your favorite reporting tools without moving data.
Learn More →Automate Excel Integration Tasks from PowerShell
Are you looking for a quick and easy way to access Excel data from PowerShell? We show how to use the Cmdlets for Excel and the CData ADO.NET Provider for Excel to connect to Excel data and synchronize, automate, download, and more.
The CData Cmdlets for Excel are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Excel.
Cmdlets or ADO.NET?
The cmdlets are not only a PowerShell interface to the Excel API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Excel data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Excel. To access Excel data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Excel.
After obtaining the needed connection properties, accessing Excel data in PowerShell consists of three basic steps.
The ExcelFile, under the Authentication section, must be set to a valid Excel File.
PowerShell
-
Install the module:
Install-Module ExcelCmdlets
-
Connect:
$excel = Connect-Excel -Excel File "$Excel File"
-
Search for and retrieve data:
$name = "Bob" $sheet = Select-Excel -Connection $excel -Table "Sheet" -Where "Name = `'$Name`'" $sheet
You can also use the Invoke-Excel cmdlet to execute SQL commands:
$sheet = Invoke-Excel -Connection $excel -Query 'SELECT * FROM Sheet WHERE Name = @Name' -Params @{'@Name'='Bob'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Excel\lib\System.Data.CData.Excel.dll")
-
Connect to Excel:
$conn= New-Object System.Data.CData.Excel.ExcelConnection("Excel File='C:/MyExcelWorkbooks/SampleWorkbook.xlsx';") $conn.Open()
-
Instantiate the ExcelDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, Revenue from Sheet" $da= New-Object System.Data.CData.Excel.ExcelDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.revenue }
Update Excel Data
PowerShell
Update-Excel -Connection $Excel -Columns @('Name','Revenue') -Values @('MyName', 'MyRevenue') -Table Sheet -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Excel.ExcelCommand("UPDATE Sheet SET Name='Bob' WHERE Id = @myId", $conn)
$cmd.Parameters.Add(new System.Data.CData.Excel.ExcelParameter("@myId","10456255-0015501366"))
$cmd.ExecuteNonQuery()
Insert Excel Data
PowerShell
Add-Excel -Connection $Excel -Table Sheet -Columns @("Name", "Revenue") -Values @("MyName", "MyRevenue")
ADO.NET
$cmd = New-Object System.Data.CData.Excel.ExcelCommand("INSERT INTO Sheet (Name) VALUES (@myName)", $conn)
$cmd.Parameters.Add(new System.Data.CData.Excel.ExcelParameter("@myName","Bob"))
$cmd.ExecuteNonQuery()
Delete Excel Data
PowerShell
Remove-Excel -Connection $Excel -Table "Sheet" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Excel.ExcelCommand("DELETE FROM Sheet WHERE Id=@myId", $conn)
$cmd.Parameters.Add(new System.Data.CData.Excel.ExcelParameter("@myId","001d000000YBRseAAH"))
$cmd.ExecuteNonQuery()
CodeProject