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

33 lines
No EOL
1.9 KiB
PowerShell
Raw 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 Export-Excel {
<#
.SYNOPSIS
Module for out PowersSell Object to Excel table
.DESCRIPTION
Example:
$Service = Get-Service
Export-Excel $Service -Path "$home\Desktop\out.xlsx" # path default
.LINK
https://github.com/Lifailon
#>
Param (
[Parameter(Mandatory = $True)] $Object,
$Path = "$home\Desktop\out.xlsx"
)
$TempCSV = "$env:TEMP\ConvertTo-Excel.csv"
$Delimiter=","
$Object | Export-Csv $TempCSV -Append -Encoding Default -Delimiter $Delimiter
$temp = (cat $TempCSV)[1..10000]
$temp > $TempCSV
$Excel = New-Object -ComObject excel.application
$WorkBook = $Excel.WorkBooks.Add(1)
$WorkSheet = $WorkBook.WorkSheets.Item(1)
$TxtConnector = ("TEXT;" + $TempCSV)
$Connector = $WorkSheet.QueryTables.add($TxtConnector,$WorkSheet.Range("A1"))
$QueryTables = $WorkSheet.QueryTables.item($Connector.name)
$QueryTables.TextFileOtherDelimiter = $Delimiter
$QueryTables.Refresh()
$QueryTables.Delete()
$WorkBook.SaveAs($Path)
$Excel.Quit()
Remove-Item $TempCSV
}