Edit Excel File using PowerShell
Problem
In this blog post, we will show you how to edit excel file using PowerShell.
As the context, before editing the file, our original Excel sheet will look as follows:
And we will edit the file such that there will be a new FirstName
and LastName
at the third row as follows:
Using Excel Com Object
Since PowerShell is deeply integrated with .NET Framework, we can use ComObject
to work with Excel files. After creating the object, we will open the Excel file to read the file and then modify the cells.
try {
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open("C:\Scripts\Test.xlsx")
$worksheet = $workbook.Worksheets.Item(1)
$worksheet.Cells.Item(3, 1).Value = "Michael"
$worksheet.Cells.Item(3, 2).Value = "Jackson"
$workbook.Save()
}
finally {
$workbook.Close()
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
}
We enclose the code above with try-finally
block because we want to make sure it will release the resource after modifying Excel that is performed at finally
block.
Using ImportExcel Module
ImportExcel
is an external module that is built based on EPPlus which is a very well-known library to work with Excel spreadsheets in .NET.
Before using ImportExcel
module, we have to install it.
Install-Module -Name ImportExcel
We also have to import the module before creating Excel object. Then following previous solution pattern, we have to open the file first to read before modifying the cells.
# Import the module
Import-Module ImportExcel
# Open excel file
$excel = Open-ExcelPackage -Path "C:\Scripts\Test.xlsx"
# Get the first worksheet and modify cells at A3 and B3
$workSheet = $excel.Workbook.Worksheets[1]
$workSheet.Cells["A3"].Value = "Michael"
$workSheet.Cells["B3"].Value = "Jackson"
# Close excel file
Close-ExcelPackage $excel
Using PSExcel Module
PSExcel
is another module based on EPPlus library. You can find all the examples in github repository.
Before using this module, we have to install it.
Install-Module -Name PSExcel
In order to use the object, we must import the module first. Then, we create Excel object by specifying the path of our Excel file. This object will be used to get the first worksheet which is at index 0. Next, we modify the cells value before saving the modifications using Save-Excel
cmdlet.
Import-Module PSExcel
$excel = New-Excel -Path 'C:\Scripts\Test.xlsx'
$workSheet1 = $excel | Get-Worksheet | Select-Object -Index 0
$workSheet1 | Set-CellValue -Coordinates "A3:A3" -Value "Michael"
$workSheet1 | Set-CellValue -Coordinates "B3:B3" -Value "Jackson"
$excel | Save-Excel -Passthru
Conclusion
To edit Excel file in PowerShell, we can use Excel Com Object
which is based on .NET Framework. We can also use PowerShell external modules like PSExcel
and ImportExcel
. ImportExcel
is an excellent module which is based on EPPlus
library, a well-known C# libary for woking with Excel from .NET.
PSExcel
is the alternative of ImportExcel
module. PSExcel
is also based on EPPlus
but this module is no longer maintained as stated in its GitHub repository. If it doesn’t provide functionality that you need, you have to contribute to the source code by yourself.