VOOZH about

URL: https://www.cdata.com/kb/tech/excel-ado-powershell.rst

⇱ Automate Excel Integration Tasks from PowerShell


Automate Excel Integration Tasks from PowerShell

👁 Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access Excel data from PowerShell? This article demonstrates how to utilize the Excel Cmdlets for tasks like connecting to Excel data, automating operations, downloading data, 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.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to Excel, 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.

Once you have acquired the necessary connection properties, accessing Excel data in PowerShell can be enabled in three steps.

Connecting to Local or Cloud-Stored (Box, Google Drive, Amazon S3, SharePoint) Excel Files

CData Drivers let you work with Excel files stored locally and stored in cloud storage services like Box, Amazon S3, Google Drive, or SharePoint, right where they are.

Setting connection properties for local files

Set the URI property to local folder path.

Setting connection properties for files stored in Amazon S3

To connect to Excel file(s) within Amazon S3, set the URI property to the URI of the Bucket and Folder where the intended Excel files exist. In addition, at least set these properties:

  • AWSAccessKey: AWS Access Key (username)
  • AWSSecretKey: AWS Secret Key

Setting connection properties for files stored in Box

To connect to Excel file(s) within Box, set the URI property to the URI of the folder that includes the intended Excel file(s). Use the OAuth authentication method to connect to Box.

Dropbox

To connect to Excel file(s) within Dropbox, set the URI proprerty to the URI of the folder that includes the intended Excel file(s). Use the OAuth authentication method to connect to Dropbox. Either User Account or Service Account can be used to authenticate.

SharePoint Online (SOAP)

To connect to Excel file(s) within SharePoint with SOAP Schema, set the URI proprerty to the URI of the document library that includes the intended Excel file. Set User, Password, and StorageBaseURL.

SharePoint Online REST

To connect to Excel file(s) within SharePoint with REST Schema, set the URI proprerty to the URI of the document library that includes the intended Excel file. StorageBaseURL is optional. If not set, the driver will use the root drive. OAuth is used to authenticate.

Google Drive

To connect to Excel file(s) within Google Drive, set the URI property to the URI of the folder that includes the intended Excel file(s). Use the OAuth authentication method to connect and set InitiateOAuth to GETANDREFRESH.

PowerShell

  1. Install the module:

    Install-Module ExcelCmdlets
  2. Connect:

    $excel = Connect-Excel -URI "$URI"
    
  3. 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

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Excel\lib\System.Data.CData.Excel.dll")
     
  2. Connect to Excel:

     
    $conn= New-Object System.Data.CData.Excel.ExcelConnection("URI='C:/MyExcelWorkbooks/SampleWorkbook.xlsx';")
    $conn.Open()
    
  3. 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-Object 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-Object 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-Object System.Data.CData.Excel.ExcelParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject

Ready to get started?

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

 Download Now

Learn more:

👁 Microsoft Excel Icon
Excel ADO.NET Provider

Easily connect .NET applications with real-time data from Excel spreadsheets. Use Excel to manage the data that powers your applications.