Hello All,
I'm new to OpenXML and my objective is to read an excel document using Powershell. The reading of the document will be on a server where excel is not installed hence the use of OpenXML. The challenge I'm having is understanding the object model in relation to using it in powershell. Below is what I have written so far and I'm stuck at the point of reading each cell. Even though I have content in the excel file it does not appear when I loop through the cells for a given row as illustrated below. Any ideas what I'm doing incorrectly below? This needs to be written in powershell and also to use openxml. The plan is to ultimately use powertools for openxml for other objectives. Thank you for your help
cls[Reflection.Assembly]::LoadWithPartialName("DocumentFormat.OpenXml")
[Reflection.Assembly]::LoadWithPartialName("DocumentFormat.OpenXml.Packaging")
[Reflection.Assembly]::LoadWithPartialName("DocumentFormat.OpenXml.Spreadsheet")
[Reflection.Assembly]::LoadWithPartialName("OpenXmlPowerTools")
[DocumentFormat.OpenXml.Packaging.SpreadsheetDocument]$Document = $null
$Document = [DocumentFormat.OpenXml.Packaging.SpreadsheetDocument]::Open("C:\temp\Book1.xlsx", $true)
[DocumentFormat.OpenXml.Packaging.WorkbookPart]$WorkBookPart = $Document.WorkbookPart
[DocumentFormat.OpenXml.Spreadsheet.Sheets]$WorkSheets = $null
$WorkSheets = $WorkBookPart.Workbook.Sheets
foreach ($WorkSheet in $WorkSheets)
{
[DocumentFormat.OpenXml.Spreadsheet.SheetData]$SheetData = $null
$SheetData = $WorkSheet.get_FirstChild()
[DocumentFormat.OpenXml.Spreadsheet.Row]$Row = $null
foreach ($Row in $SheetData)
{
[DocumentFormat.OpenXml.Spreadsheet.Cell]$Cell = $null
foreach ($Cell in $Row)
{
Write-Host $Cell.CellValue.Text
}
}
}
$Document.Close()