Created
October 20, 2020 16:49
-
-
Save hirenchauhan2/9b690b6f01c148de215141bb1d1dcc37 to your computer and use it in GitHub Desktop.
Read excel file from Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Declare the file path and sheet name | |
$file = "C:\Excelfile.xlsx" | |
# name of the sheet you want to read | |
$sheetName = "Sheet1" | |
#Create an instance of Excel.Application and Open Excel file | |
$objExcel = New-Object -ComObject Excel.Application | |
$workbook = $objExcel.Workbooks.Open($file) | |
$sheet = $workbook.Worksheets.Item($sheetName) | |
$objExcel.Visible = $false | |
#Count max rows | |
$rowMax = ($sheet.UsedRange.Rows).count | |
#Declare the starting positions | |
$rowName, $colName = 1, 1 | |
$rowName, $colAge = 1, 2 | |
#loop to get values from rows | |
for ($i = 1; $i -le $rowMax - 1; $i++) { | |
$name = "COMMUNITY\" + $sheet.Cells.Item($rowName + $i, $colName).text | |
$age = $sheet.Cells.Item($rowName + $i, $colAge).text | |
Write-Host ("Name is: " + $name) | |
Write-Host ("Age is: " + $age) | |
Write-Host ("-----------------") | |
} | |
#close excel file | |
$objExcel.quit() | |
Write-Host "Press any key to continue..." | |
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment