PS-Commands/Scripts/Import-Excel.psm1
2023-10-14 12:06:13 +03:00

45 lines
No EOL
2.2 KiB
PowerShell
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function Import-Excel {
<#
.SYNOPSIS
Module for import Excel table to Hashtable
.DESCRIPTION
Example:
Import-Excel -Path "$home\Desktop\out.xlsx"
.LINK
https://github.com/Lifailon
#>
Param (
[Parameter(Mandatory = $True)]$Path
)
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$ExcelWorkBook = $excel.Workbooks.Open($path)
$ListsIndex = $ExcelWorkBook.Sheets | select Index
$Index1 = $ListsIndex[0].Index
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item($Index1)
$Range = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$Arr = $Range.ToCharArray()
$Columns = New-Object System.Collections.Generic.List[System.Object]
$Arr | %{
if ($ExcelWorkSheet.Range("$_"+"1").Text.Length -ne 0) {
$Columns.Add([PSCustomObject]@{
Column = $_;
Value = $ExcelWorkSheet.Range("$_"+"1").Text
})
}
}
foreach ($Col in $Columns) {
$Column = $Col.Column
$Value = $Col.Value
$ColVal = @()
2..1000 | %{
if ($ExcelWorkSheet.Range("$Column"+"$_").Text.Length -ne 0) {
$ColVal += @($ExcelWorkSheet.Range("$Column"+"$_").Text)
}
}
$ht += @{$Value = $ColVal}
}
$ht
$ExcelWorkBook.close($true)
$Excel.Quit()
}