茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1673456 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_705.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2015/12/18 2:36:50

PowerShell - Read an Excel file using COM Interface

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
}

Download



GitHub
Technet Gallery

Other References


Thanks for reading! If you have any questions, leave a comment or send me an email at fxcat@lazywinadmin.com. I invite you to follow me on Twitter @lazywinadm / Google+ / LinkedIn. You can also follow the LazyWinAdmin Blog on Facebook Page and Google+ Page.

原文出處: LazyWinAdmin: PowerShell - Read an Excel file using COM Interface
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|