# Now get a list of all csv files in current directory :) $targetcsv=dir *.csv #Create a new Excel object and add a new workbook. $Excel = New-Object -ComObject excel.application $Excel.visible = $true $workbooks = $excel.Workbooks.Add() $worksheets = $workbooks.worksheets #Delete the extra worksheets and rename the first worksheet. $worksheets.Item(3).delete() $worksheets.Item(2).delete() #Add worksheets based on the count of files $count=1 foreach ( $CSVFile in $targetcsv ){ IF ($count -ne 1){ $worksheets.Add()} #Write-Host $CSVFile.BaseName #Select worksheet $worksheet = $worksheets.Item(1) #Give it a updated name $worksheet.Name = $CSVFile.BaseName #Grab the CSV $TxtConnector = ("TEXT;" + $CSVfile.fullname) $CellRef = $worksheet.Range("A1") #Import the text file $Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef) $worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet.QueryTables.item($Connector.name).Refresh() $worksheet.QueryTables.item($Connector.name).delete() #make pretty $worksheet.UsedRange.EntireColumn.AutoFit() #loop for fun! $count=$count+1 write-host $count }
Monday, June 17, 2013
Add\Import multiple CSV to excel
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment