Skip to content

Instantly share code, notes, and snippets.

@hsupu
Created November 24, 2019 18:02
Show Gist options
  • Save hsupu/7853a138578c2f0156fd5cd24bd6fafe to your computer and use it in GitHub Desktop.
Save hsupu/7853a138578c2f0156fd5cd24bd6fafe to your computer and use it in GitHub Desktop.
A pwsh example to read excel
$xls = New-Object -ComObject excel.application
$xls.visible = $true
$xls.displayAlerts = $false
$workbook = $xls.workbooks.open("Z:\Downloads\W020170213333264378621.xls")
$sheet = $workbook.sheets.item(1)
function GetValue($shell, $row, $col) {
return $sheet.cells.item($row, $col).Text
}
"" | Out-File "output.txt"
for ($i = 4; $i -le 2629; $i += 1) {
try {
$id = [int]::parse((GetValue $sheet $i 1))
} catch {
continue
}
$school = GetValue $sheet $i 2
$department = GetValue $sheet $i 3
$location = GetValue $sheet $i 4
$level = GetValue $sheet $i 5
$private = (GetValue $sheet $i 6) -ne $null
$line = "{0}`t{1}`t{2}`t{3}`t{4}`n" -f $id, $school, $department, $location, $level, $private
$line | Out-File "output.txt" -Append
}
$workbook.Close()
$xls.Application.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment