Ready to get started?

Learn more about the Excel Online PowerShell Cmdlets or download a free trial:

Download Now

PowerShell Scripting to Replicate Excel Online Data to MySQL

Write a simple PowerShell script to replicate Excel Online data to a MySQL database.

The CData Cmdlets for Excel Online offer live access to Excel Online data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for Excel Online and the CData Cmdlets for MySQL in PowerShell to replicate Excel Online data to a MySQL database.

After obtaining the needed connection properties, accessing Excel Online data in PowerShell and preparing for replication consists of four 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.

Collecting Excel Online Data

  1. Install the module:

    Install-Module ExcelOnlineCmdlets
  2. Connect to Excel Online:

    $excelonline = Connect-ExcelOnline
  3. Retrieve the data from a specific resource:

    $data = Select-ExcelOnline -Connection $excelonline -Table "Test_xlsx_Sheet1"

    You can also use the Invoke-ExcelOnline cmdlet to execute pure SQL-92 statements:

    $data = Invoke-ExcelOnline -Connection $excelonline -Query 'SELECT * FROM Test_xlsx_Sheet1 WHERE Column2 = @Column2' -Params @{'@Column2'='Bob'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting Excel Online Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the Excel Online data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the Excel Online resource (Test_xlsx_Sheet1) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Test_xlsx_Sheet1" -Columns $columns -Values $values }

You have now replicated your Excel Online data to a MySQL database. This gives you freedom to work with Excel Online data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to Excel Online and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-ExcelOnline -Connection $excelonline -Table "Test_xlsx_Sheet1" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Test_xlsx_Sheet1" -Columns $columns -Values $values }
  • If you wish to replicate the Excel Online data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-ExcelOnline cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}