In this blog, I want to discuss a few commands that can be used when we have to do an update on Excel cells using PowerShell. Recently I was working on a script for updating an exported Excel document, and finding commands for various functions in one place was difficult. Here are a few commands that are helpful for updating an Excel document using the power of PowerShell:
$excel = New-Object -Com Excel.Application
$wb = $excel.Workbooks.Add()
$ws = $wb.Worksheets.Add()
$excel = New-Object -Com Excel.Application
$importFile = 'C:\Users\User\Downloads\exceldoc.xlsx'
#Open the importfile for edit.
$wb = $excel.Workbooks.Open($importFile)
$ws = $wb.sheets.item(1)
$ws.name = "Asset"
This could be used in iterating through the used range in a worksheet.
$ws.UsedRange.columns.count
$ws.UsedRange.rows.count
$header= $ws.Cells.Item(1, 1).text
#update the cell with 'New value'
$ws.Cells.Item(1,1).Value="New value"
[void]$ws.Cells.Item(1,1).EntireColumn.Delete()
[void]$ws.Cells.Item(1,1).EntireRow.Delete()
$excel.Save()
$excel.Quit()
These commands can be used together to mass update the cells based on conditions and iterations. You could add many more to the above list, but the ones mentioned are the ones I use and find most helpful for mass updations.
Thanks for reading!
Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.