Routine on converting CSV file into XLSX file.
Function ConvertToExcel($csvPath , $excelPath, $header)
{
# Start Excel application
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.worksheets.Item(1)
# Import CSV data into the worksheet
if ($header)
{
$row = 2
}
else
{
$row = 1
}
Import-Csv $csvPath | ForEach-Object {
$col = 1
foreach ($value in $_.PSObject.Properties.Value) {
$worksheet.Cells.Item($row, $col) = $value
$col++
}
$row++
}
# Add column headers manually
if ($header)
{
$csvHeaders = (Get-Content $csvPath -TotalCount 1).Split(',')
for ($i = 0; $i -lt $csvHeaders.Count; $i++) {
$worksheet.Cells.Item(1, $i + 1) = $csvHeaders[$i]
}
}
# Save and close Excel workbook
$workbook.SaveAs($excelPath)
$excel.Quit()
}
Source
https://www.delftstack.com/howto/powershell/export-a-csv-to-excel-using-powershell/