Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

combine 2 csv files into one excel file and save it...

$
0
0

hello all - thanks for the time and effort.

i am trying to figure out how through powershell to combine 2 csv files into one excel file...with each having its own sheet.  i found this script below..and it works. however it doesnt save the file. ive tried a bit to see if i can save it by adding some of my logic in there .... but of course my logic makes no sense. :) .  i just don't know what to add to save it at the end.  any and all help is appreciated.

this was posted by lmfancisd:

#######

# For combining excel type files, I tend to do the following:

#

# Workbooks.Add(1)

# - create workbook with 1 worksheet

# - can't create a workbook with 0 worksheets

# - so I delete this one in the end

#

# Workbooks.Add(filepath)

# - use Add instead of Open for reading files

# - Add uses filepath as a template, so changes

# - will not be saved to original file

# - Excel understands .csv, so I don't worry

# - about delimeters and things like that

#

# worksheet1.Copy(worksheet2)

# - copy worksheet1

# - put copy before worksheet2

# - worksheet1 and worksheet2 can be in different workbooks

 

$xl = New-Object -ComObject Excel.Application;

$xl.Visible = $true;

$wb = $xl.Workbooks.Add(1);

$ws = $wb.Worksheets.Item(1);

 

gci *.csv | ForEach-Object {

    $wbCsv = $xl.Workbooks.Add($_.FullName);    

    $wsCsv = $wbCsv.Worksheets.Item(1);

    $wsCsv.Copy($ws);

    $wbCsv.Close($false);

}

 

$ws.Delete();

$wb.Worksheets.Item(1).Select();

xlbook.save

#####


Viewing all articles
Browse latest Browse all 6937

Trending Articles