PowerShell works with Excel spreadsheets

by Doug Finke on August 19, 2007

in Excel, PowerShell

Get-ExcelData Script

Get-ExcelData

This script reads Excel spreadsheets.

It takes two parameters

The Excel file name and an ‘sql command’.

The provider used in the connection treats the worksheet like a database.

Example WorkSheet

ExcelSheet

This sheet is named Sheet1. You access the data with an sql command.

  Excel Sql

Using Excel data in calculations

The previous example printed the contents of the Excel sheet. Here is how you can programmatically work with the data.

  ExcelCalc

Using the script, the columns StandardHours and Wage were extracted from the spreadsheet. Then, using the PowerShell ForEach, looped through and the Pay calculated.

Wrapping up

This post outlines how easy it is to access Excel spreadsheets with PowerShell.

Upcoming posts will use the Get-ExcelData script to

  • Do code generation
  • Simulate FIT (Framework for Integrated Test)
  • Write data to a spreadsheet
  • Create a spreadsheet on the fly
  • Access Excel Named Ranges
Downloads

Download the script and test spreadsheet here.


Links

The Get-ExcelData script is a pared down version of Lee Holmes Invoke-SqlCommand.

You can find it in his upcoming book Windows PowerShell: The Definitive Guide.

{ 1 trackback }

Tweets that mention PowerShell works with Excel spreadsheets -- Topsy.com
12.22.09 at 12:07 pm

{ 2 comments… read them below or add one }

1 Luke Flemmer 08.19.07 at 1:39 pm

Great post. This is a good example of a syntax just doing what you might hope it would :-)

2 Grant 01.02.09 at 2:47 pm

now this would be M

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>