Automate Excel Online Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Excel Online ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with live Excel Online Spreadsheet data!



Are you looking for a quick and easy way to access Excel Online data from PowerShell? We show how to use the Cmdlets for Excel Online and the CData ADO.NET Provider for Excel Online to connect to Excel Online data and synchronize, automate, download, and more.

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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Excel Online API, 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.

After obtaining the needed connection properties, accessing Excel Online data in PowerShell consists of three basic steps.

You can connect to a workbook by providing authentication to Excel Online and then setting the following properties:

  • Workbook: 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.

  • UseSandbox: Set this to true if you are connecting to a workbook in a sandbox account. Otherwise, leave this blank to connect to a production account.

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.

PowerShell

  1. Install the module:

    Install-Module ExcelOnlineCmdlets
  2. Connect:

    $excelonline = Connect-ExcelOnline
  3. 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'}

ADO.NET

  1. 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")
  2. Connect to Excel Online:

    $conn= New-Object System.Data.CData.ExcelOnline.ExcelOnlineConnection("InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. 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 Excel Online Data

PowerShell

Update-ExcelOnline -Connection $ExcelOnline -Columns @('Id','Column1') -Values @('MyId', 'MyColumn1') -Table Test_xlsx_Sheet1 -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.ExcelOnline.ExcelOnlineCommand("UPDATE Test_xlsx_Sheet1 SET Column2='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.ExcelOnline.ExcelOnlineParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Excel Online Data

PowerShell

Add-ExcelOnline -Connection $ExcelOnline -Table Test_xlsx_Sheet1 -Columns @("Id", "Column1") -Values @("MyId", "MyColumn1")

ADO.NET

$cmd = New-Object System.Data.CData.ExcelOnline.ExcelOnlineCommand("INSERT INTO Test_xlsx_Sheet1 (Column2) VALUES (@myColumn2)", $conn) $cmd.Parameters.Add(new System.Data.CData.ExcelOnline.ExcelOnlineParameter("@myColumn2","Bob")) $cmd.ExecuteNonQuery()

Delete Excel Online Data

PowerShell

Remove-ExcelOnline -Connection $ExcelOnline -Table "Test_xlsx_Sheet1" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.ExcelOnline.ExcelOnlineCommand("DELETE FROM Test_xlsx_Sheet1 WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.ExcelOnline.ExcelOnlineParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()