简体   繁体   中英

Powershell Function import multiple CSVs into Excel workbook

Ok, time to start this question over. I found the following script http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/09/copy-csv-columns-to-an-excel-spreadsheet-by-using-powershell.aspx

My question is how would I get powershell to loop that script for any number of CSV files. The speed of the script is not important. I've received some answers of which, in some way, have their problems. Using the Technet script provides the correct output.

I have come up with this but am having a tough time getting the code to loop through multiple CSV files.

Function Excel-Stuff {
[cmdletBinding()]
Param([Parameter(ValueFromPipeline=$true)][string]$junk)
$excel.cells.item(1,1) = "Server"
$excel.cells.item(1,2) = "Rack"
$excel.cells.item(1,3) = "Environment"
$excel.cells.item(1,4) = "RebootTime"
$excel.cells.item(1,5) = "Schedule"
$i = 2
$processes = Import-Csv 'C:\Monday.csv'
foreach ($process in $processes){
 $excel.cells.item($i,1) = $process.Server
 $excel.cells.item($i,2) = $process.Rack
 $excel.cells.item($i,3) = $process.Environment
 $excel.cells.item($i,4) = $process.RebootTime
 $excel.cells.item($i,5) = $process.Schedule
 $i++
} #end foreach process
$autofit = $Global:worksheet.UsedRange
$autofit.EntireColumn.AutoFit() | Out-Null
}#End Function.
$Excel = New-Object -ComObject excel.application
$workbook = $Excel.workbooks.add(1)
$Global:worksheet = $workbook.WorkSheets.Item(1)
$Global:worksheet.Name='Monday'
Excel-Stuff
$Excel.visible = $True

Ok, so let's not do this the hard way. Copy/paste is your friend, and you can do it easily here.

You have Monday-Friday tabs, and I assume 7 CSV files. Loop this thing 7 times, and in each loop Create a tab, name it, then copy the entire CSV, convert it to a tab delimited CSV, and pipe it to CLIP.exe. Then just select A1 on the current sheet and paste. Start the next loop with the next CSV file.

$path = "c:\tmp\mytest.xlsx"
$day = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$workbook = $Excel.workbooks.add(1)

$day | %{
 $processes = Import-Csv -Path "D:\Scripts\work\$_.csv"
 $worksheet = $workbook.WorkSheets.add()
 $processes | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Clip.exe
 $worksheet.select()
 $worksheet.Name = $_
 [void]$Excel.ActiveSheet.Range("A1:A1").Select()
 [void]$Excel.ActiveCell.PasteSpecial()
 [void]$worksheet.UsedRange.EntireColumn.AutoFit()
}

#Clean up extra sheets
$Excel.DisplayAlerts = $false
$workbook.Worksheets|?{$day -notcontains $_.name}|%{$_.Delete()}
$Excel.DisplayAlerts = $true

[void]$Excel.ActiveSheet.Range("A1:A1").Select()
$workbook.saveas($path)
$Excel.Quit()
Remove-Variable -Name excel
[gc]::collect()
[gc]::WaitForPendingFinalizers()

Then your only concern is that the CSV files are in the right order. If you named your CSV files Monday.csv, Tuesday.csv etc this could probably be simpler, but so long as they're named so that Monday's is the first, and it goes in alpha-numeric order you'll be just fine with that code.

(Edited to reflect comment suggestions for single cell selection before saving and changed -notin to -notcontains)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM