Automate Excel Online Integration Tasks from PowerShell

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access Excel Online data from PowerShell? This article demonstrates how to utilize the Excel Online Cmdlets for tasks like connecting to Excel Online data, automating operations, downloading data, and more.

The CData ADO.NET Provider for Excel Online is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Excel Online.

ADO.NET Provider

The ADO.NET Provider provides a SQL interface for Excel Online; this tutorial shows how to use the Provider to create, retrieve, update, and delete Excel Online data.

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:

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

  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


$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()

Insert Excel Online Data


$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()

Delete Excel Online Data


$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()

Ready to get started?

Download a free trial of the Excel Online Data Provider to get started:

 Download Now

Learn more:

Excel Online Icon Excel Online ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Excel Online data including Workbooks, Sheets, and more!