![]() |
VOOZH | about |
The CData Cmdlets for Excel Online 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 Online.
The Cmdlets are not only a PowerShell interface to Excel Online, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Excel Online data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Excel Online. To access Excel Online data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Excel Online.
Once you have acquired the necessary connection properties, accessing Excel Online data in PowerShell can be enabled in three steps.
You can connect to a workbook by providing authentication to Excel Online and then setting the following properties:
: Set this to the name or Id of the workbook.
If you want to view a list of information about the available workbooks, execute a query to the Workbooks view after you authenticate.
You use the OAuth authentication standard to authenticate to Excel Online. See the Getting Started section in the help documentation for a guide. Getting Started also guides you through executing SQL to worksheets and ranges.
Install the module:
Install-Module ExcelOnlineCmdlets
Connect:
$excelonline = Connect-ExcelOnline -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$column2 = "Bob" $test_xlsx_sheet1 = Select-ExcelOnline -Connection $excelonline -Table "Test_xlsx_Sheet1" -Where "Column2 = `'$Column2`'" $test_xlsx_sheet1
You can also use the Invoke-ExcelOnline cmdlet to execute SQL commands:
$test_xlsx_sheet1 = Invoke-ExcelOnline -Connection $excelonline -Query 'SELECT * FROM Test_xlsx_Sheet1 WHERE Column2 = @Column2' -Params @{'@Column2'='Bob'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Excel Online\lib\System.Data.CData.ExcelOnline.dll")
Connect to Excel Online:
$conn= New-Object System.Data.CData.ExcelOnline.ExcelOnlineConnection("InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the ExcelOnlineDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Column1 from Test_xlsx_Sheet1"
$da= New-Object System.Data.CData.ExcelOnline.ExcelOnlineDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.id $_.column1
}
Update-ExcelOnline -Connection $ExcelOnline -Columns @('Id','Column1') -Values @('MyId', 'MyColumn1') -Table Test_xlsx_Sheet1 -Id "MyId"
$cmd = New-Object System.Data.CData.ExcelOnline.ExcelOnlineCommand("UPDATE Test_xlsx_Sheet1 SET Column2='Bob' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ExcelOnline.ExcelOnlineParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-ExcelOnline -Connection $ExcelOnline -Table Test_xlsx_Sheet1 -Columns @("Id", "Column1") -Values @("MyId", "MyColumn1")
$cmd = New-Object System.Data.CData.ExcelOnline.ExcelOnlineCommand("INSERT INTO Test_xlsx_Sheet1 (Column2) VALUES (@myColumn2)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ExcelOnline.ExcelOnlineParameter("@myColumn2","Bob")))
$cmd.ExecuteNonQuery()
Remove-ExcelOnline -Connection $ExcelOnline -Table "Test_xlsx_Sheet1" -Id "MyId"
$cmd = New-Object System.Data.CData.ExcelOnline.ExcelOnlineCommand("DELETE FROM Test_xlsx_Sheet1 WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ExcelOnline.ExcelOnlineParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Excel Online Data Provider to get started:
Download NowLearn more:
👁 Excel Online IconRapidly create and deploy powerful .NET applications that integrate with live Excel Online Spreadsheet data!