Hi, I have used a script from another site which combines csv files into excel document. Works pretty well, but it seems to be adding the data from the fist sheet into the next and in the sheets after that. Can’t seem to see an issue with the script after loads of tests.
Function MergeCSV { $csvs = Get-ChildItem .* -Include *.csv $csvs = $csvs | where length -NotContains 0 $y=$csvs.Count Write-Host “Detected the following CSV files: ($y)” foreach ($csv in $csvs) { Write-Host ” “$csv.Name } $Date = (Get-Date -Format 'ddMMyy_HHmm') $outputfilename = "$PSScriptRootAll_Servers_Report_$date" Write-Host Creating: $outputfilename $excelapp = new-object -comobject Excel.Application $excelapp.sheetsInNewWorkbook = $csvs.Count $xlsx = $excelapp.Workbooks.Add() $sheet=1 $count = 1 foreach ($csv in $csvs) { $name = $csv.name Write-Progress -id 1 -Activity "Creating sheet for $name ($count of $y)" -PercentComplete ((($Count)/$Csvs.Count) * 100) $Count++ ## -CurrentOperation "Creating sheet $count out of $y" $time = (Get-Date -Format 'HHmm') $row=1 $column=1 $worksheet = $xlsx.Worksheets.Item($sheet) $worksheet.Name = $csv.Name.Split('_')[1] + "_$time" $file = (Get-Content $csv) $sheet++ $count2 = 1 #------------------------------ # Create Lines foreach($line in $file){ Write-Progress -id 2 -parentId 1 -Activity "Creating lines" -PercentComplete ((($count2)/$file.Count) * 100) $Count2++ ## -CurrentOperation "Creating Lines" $linecontents=$line -split ‘,(?!s*w+”)’ #------------------------------ # Create cells for sheet foreach($cell in $linecontents){ $cell = $cell.Trim('"') $worksheet.Cells.Item($row,$column) = $cell $column++ } $column=1 $row++ } } $xlsx.SaveAs($outputfilename) $excelapp.quit() } MergeCSV
submitted by /u/Mskews
[link] [comments]
The post Combine multiple CSV files into an XLSX file appeared first on How to Code .NET.