I have a script that creates x amount of csv's I want to convert all the csv's in the folder into one Xlsx file.
I have the basics of cycling through each csv, however it overwirtes after the end of each loop.
I have want to add each csv to a worksheet in the workbook and save it.
but it only displays the last csv it read and doesn't show any other worksheets.
I have sampled the code from the following;
How to export a CSV to Excel using Powershell
As discussed, I have tried the above; no luck.
Function Excel-Write{
$RD = "C:\temp\Servers\*.csv"
$CsvDir = $RD
$csvs = dir -path $CsvDir
$outputxls = ""C:\temp\Servers\Complete.Xlsx"
foreach($iCsv in $csvs){
$WB = $csvs.Length
$Excel = New-Object -ComObject excel.application
#$Excel.displayAlerts = $false
$workbook = $excel.Workbooks.add(1)
$Worksheet = $workbook.worksheets.Item(1)
$Worksheet.name = $Group
$TxtConnector = ("TEXT;" + $iCsv)
$Connector = $worksheet.Querytables.add($txtconnector,$worksheet.Range("A1"))
$query = $Worksheet.QueryTables.item($Connector.name)
$query.TextfileOtherDelimiter = $Excel.Application.International(5)
$Query.TextfileParseType =1
$Query.TextFileColumnDataTypes = ,2 * $worksheet.cells.column.count
$query.AdjustColumnWidth =1
$Query.Refresh()
$Query.Delete()
$workbook.Save()
}
$Workbook.SaveAs($outputxls,51)
$Excel.quit()
}
Only shows one worksheet and this is the last CSV that has been read, it overwrites other worksheets.