PowerShell - Read an Excel file using COM Interface

http://www.lazywinadmin.com/2014/03/powershell-read-excel-file-using-com.html

Last week, I worked on a small PowerShell script to read a custom excel file sheet with a lot of information in different columns and rows. Unfortunately, you‘ll have to adapt the script to your needs. However I thought this might be helpful to understand how this work.

Here is the sample Excel file I worked with:

COM interface: Excel.Application

Layers
To access an Excel file data, you have to be aware of the hierarchy of each elements/layers.
The first element will be the application class (at the the top) that contains one or more workbooks, each workbooks contains one or more worksheets, inside each of the worksheet you can access ranges. Each element can access down to some of the other layers.



Microsoft Excel - Layers

Application layer
This will open a new instance of excel on my computer as you can see below:

$objExcel
=
New-Object
-ComObject Excel.Application

MSDN reference: Microsoft.Office.Interop.Excel.ApplicationClass



A new Excel Instance is created

Workbook layer
In the second step, we are opening the workbook inside the Excel Instance.

$WorkBook
=
$objExcel.Workbooks.Open("C:\VIDEOSERVER01-BuildSpecs.xlsx")

We can verify that the workbook is opened using the following line

$objExcel.WorkBooks |
Select-Object
-Property name, path, author

Then see the properties and methods that can be used

$objExcel.WorkBooks |
Get-Member

In the following example, I hold the data inside the $WorkBook variable.
$WorkBook = $objExcel.Workbooks.Open("C:\VIDEOSERVER01-BuildSpecs.xlsx")

I then look for any member (property, method, or event) that contains the word "sheet".

$WorkBook
|
Get-Member
-Name
*sheet*

My WorkBook contains 2 worksheets as you can see in the first image of the article, "BuildSpecs" and "Sheet2", PowerShell can retrieve this information using the "sheets" property.

$WorkBook.sheets |
Select-Object
-Property Name

WorkSheet layer
We select the sheet BuildSpecs and are now able to start working on the data

$WorkSheet
=
$WorkBook.sheets.item("BuildSpecs")

Loading and getting the Data

Loading the file in PowerShell can be done with just a few lines.

# Specify the path to the Excel file and the WorkSheet Name

$FilePath
=
"C:\VIDEOSERVER01-BuildSpecs.xlsx"

$SheetName
=
"BuildSpecs"

# Create an Object Excel.Application using Com interface

$objExcel
=
New-Object
-ComObject Excel.Application

# Disable the ‘visible‘ property so the document won‘t open in excel

$objExcel.Visible =
$false

# Open the Excel file and save it in $WorkBook

$WorkBook
=
$objExcel.Workbooks.Open($FilePath)

# Load the WorkSheet ‘BuildSpecs‘

$WorkSheet
=
$WorkBook.sheets.item($SheetName)

Let‘s get the ComputerName value which is "VIDEOSERVER01" is this example.
If you look at the sheet "BuildSpecs" below, you‘ll see the ComputerName information in the C3 cell. We can also refer to it using the coordinate-systems: Third column/third row.

Different methods are possible to retrieve the information from the sheet, I found the following ones:

$worksheet.Range("C3").Text

$worksheet.Range("C3:C3").Text

$worksheet.Range("C3","C3").Text

$worksheet.cells.Item(3, 3).text

$worksheet.cells.Item(3, 3).value2

$worksheet.Columns.Item(3).Rows.Item(3).Text

$worksheet.Rows.Item(3).Columns.Item(3).Text

$worksheet.UsedRange.Range("c3").Text

Finally we can create a PowerShell object to output the information we want to extract from the Excel file.

$Output
= [pscustomobject][ordered]@{

ComputerName =
$WorkSheet.Range("C3").Text

Project =
$WorkSheet.Range("C4").Text

Ticket =
$WorkSheet.Range("C5").Text

Role =
$WorkSheet.Range("C8").Text

RoleType =
$WorkSheet.Range("C9").Text

Environment =
$WorkSheet.Range("C10").Text

Manufacturer =
$WorkSheet.Range("C12").Text

SiteCode =
$WorkSheet.Range("C15").Text

isDMZ =
$WorkSheet.Range("C16").Text

OperatingSystem =
$WorkSheet.Range("C18").Text

ServicePack =
$WorkSheet.Range("C19").Text

OSKey =
$WorkSheet.Range("C20").Text

Owner =
$WorkSheet.Range("C22").Text

MaintenanceWindow =
$WorkSheet.Range("C23").Text

NbOfProcessor =
$WorkSheet.Range("C26").Text

NbOfCores =
$WorkSheet.Range("C27").Text

MemoryGB =
$WorkSheet.Range("C29").Text

}

Script example

Here is an example with all the pieces together.

#Specify the path of the excel file

$FilePath
=
"C:\LazyWinAdmin\VIDEOSERVER01-BuildSpecs.xlsx"

#Specify the Sheet name

$SheetName
=
"BuildSpecs"

# Create an Object Excel.Application using Com interface

$objExcel
=
New-Object
-ComObject Excel.Application

# Disable the ‘visible‘ property so the document won‘t open in excel

$objExcel.Visible =
$false

# Open the Excel file and save it in $WorkBook

$WorkBook
=
$objExcel.Workbooks.Open($FilePath)

# Load the WorkSheet ‘BuildSpecs‘

$WorkSheet
=
$WorkBook.sheets.item($SheetName)

[pscustomobject][ordered]@{

ComputerName =
$WorkSheet.Range("C3").Text

Project =
$WorkSheet.Range("C4").Text

Ticket =
$WorkSheet.Range("C5").Text

Role =
$WorkSheet.Range("C8").Text

RoleType =
$WorkSheet.Range("C9").Text

Environment =
$WorkSheet.Range("C10").Text

Manufacturer =
$WorkSheet.Range("C12").Text

SiteCode =
$WorkSheet.Range("C15").Text

isDMZ =
$WorkSheet.Range("C16").Text

OperatingSystem =
$WorkSheet.Range("C18").Text

ServicePack =
$WorkSheet.Range("C19").Text

OSKey =
$WorkSheet.Range("C20").Text

Owner =
$WorkSheet.Range("C22").Text

MaintenanceWindow =
$WorkSheet.Range("C23").Text

NbOfProcessor =
$WorkSheet.Range("C26").Text

NbOfCores =
$WorkSheet.Range("C27").Text

MemoryGB =
$WorkSheet.Range("C29").Text

}

时间: 2024-08-07 08:25:45

PowerShell - Read an Excel file using COM Interface的相关文章

Apache POI – Reading and Writing Excel file in Java

来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, we will discuss about how to read and write an excel file using Apache POI 1. Basic definitions for Apache POI library This section briefly describe a

NetSuite SuiteScript 2.0 export data to Excel file(xls)

In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collect 'encoded' string to Array for column, join them with comma ',' to be a string. Collect each line's data same as column to push to the Array. Join al

csharp:OpenXml SDK 2.0 and ClosedXML read excel file

https://openxmlexporttoexcel.codeplex.com/ 引用: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DocumentForma

Formatting Excel File Using Ole2 In Oracle Forms

Below is the some useful commands of Ole2 to format excel file in Oracle Forms. -- Change font size and name in ole2-- Declare as cfont ole2.obj_type;CFONT := OLE2.GET_OBJ_PROPERTY(CELL, 'Font');OLE2.SET_PROPERTY(CFONT, 'Name','Calibri');       OLE2.

To upload an excel file using WebDynpro Application.

Scenario:To upload an excel file using WebDynpro Application. Procedure: 1.       Go to transaction SE80. 2.        Select "WebDynpro Comp./Intf" from the list. 3.       Create a new WebDynpro component by the name ZEXCEL_UPLOAD. 4.       Double

Creating Excel File in Oracle Forms

Below is the example to create an excel file in Oracle Forms. Pass the Sql query string to the below procedure to generate an Excel file using Ole2 package. PROCEDURE Create_Excel_File (CSQL Varchar2)Is   source_cursor    Integer;   l_colCnt        

Read / Write Excel file in Java using Apache POI

Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year or two ago I was working with finance team where they wanted to pull the credit card transactions for all the customer using various combinations. Ex –

How to create Excel file in C#

http://csharp.net-informations.com/excel/csharp-create-excel.htm Before you create an Excel file in C# , you have to add the Microsoft Excel 12.0 Object Library to you project.

[Java]Write Excel File Using Apache POI API

package com.file.properties; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Hashtable; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.