Insert Image to Excel Using PowerShell
Problem
In this article, we will show you how to insert image to Excel using PowerShell.
As the context, we will insert image to an empty worksheet, after which the sheet will look as follows:
Using Excel Com Object
After creating the object, we will open the Excel file to get workbook object we want to manipulate.
Then, to insert the image we use AddPicture
method from Shapes
class. The last four arguments of the method determine the location and the size of the image. We set the position 10 from top and and upper-left corner of the sheet. For image size, we set 70 as the height and width.
You can check from the documentation regarding the description of all parameters.
try {
# Open excel file
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open("C:\Scripts\Book1.xlsx")
# Get active worksheet
$sheet = $workbook.ActiveSheet
# Insert image
$imageFile = "C:\Scripts\twitter.png"
$picture = $sheet.Shapes.AddPicture($imageFile, $true, $true, 10, 10, 70, 70)
# Save excel file
$workbook.Save()
}
finally {
# Close excel file
$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
And then following previous solution pattern, we have to create Excel object first before inserting the image using AddPicture
method from Drawings
class.
You can check from the documentation regarding the description of all parameters.
try {
# Import the module
Import-Module ImportExcel
# Open excel file
$excel = Open-ExcelPackage -Path "C:\Scripts\Book1.xlsx"
# Get active worksheet
$currentWorksheetIndex = $excel.Workbook.View.ActiveTab
$sheet = $excel.Workbook.Worksheets[$currentWorksheetIndex + 1]
# Insert image
$imageFile = "C:\Scripts\twitter.png"
$fileInfo = New-Object System.IO.FileInfo $imageFile
$picture = $sheet.Drawings.AddPicture("twitter", $fileInfo)
$picture.SetPosition(10, 10)
$picture.SetSize(70, 70)
}
finally {
# Close excel file
Close-ExcelPackage $excel
}
Similar to previous solution, we enclose the script with try-finally
block to avoid memory leak.
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 insert the image using AddPicture
method from Drawings
class.
You can check from the documentation regarding the description of all parameters.
try {
# Import the module
Import-Module PSExcel
# Open excel file
$excel = New-Excel -Path 'C:\Scripts\Book1.xlsx'
# Get active worksheet
$currentWorksheetIndex = $excel.Workbook.View.ActiveTab
$sheet = $excel.Workbook.Worksheets[$currentWorksheetIndex + 1]
# Insert image
$imageFile = "C:\Scripts\twitter.png"
$fileInfo = New-Object System.IO.FileInfo $imageFile
$picture = $sheet.Drawings.AddPicture("twitter", $fileInfo)
$picture.SetPosition(10, 10)
$picture.SetSize(70, 70)
# Save excel file
$excel | Save-Excel
}
finally {
# Close excel file
$excel | Close-Excel
}
We also enclose the script with try-finally
block to avoid memory leak.
Conclusion
To insert image to Excel using PowerShell, we can use excel module/libraries to manipulate excel.
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 popular 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.