简体   繁体   中英

Copy Excel Data with the Same Name of Worksheet from Multiple Workbooks using Powershell

I don't know how to explain this, but I'm trying to create a Comparison Tool using Powershell. I have Multiple Source Files with multiple worksheets that has same names with the other files. I'm trying to copy the "VALUE" under File_1 and so as the File_2 and File_3 and paste it on the 2nd Row of the Output File depending on File Name and the WorkSheet Name and save it as a New Output File.

I'm following this post: https://stackoverflow.com/a/23742718/19614816 but these only gets the first sheet of every files and paste it in one sheet. My problem is how to get the VALUE of every worksheet under the same worksheet names and paste it in the Output File and generate a new same Worksheet name as the Source Worksheets.

Worksheet in Source Files:

源文件工作表

And another problem is idk or is there a way on how to Filter the Source Files as I only want to compare the in File_1,2,3 and same with the File_1,3,4 and so on...

Sample Template of Output File:

输出文件

Sample Source File:

源文件

Thank you for taking time for this problem:)

I thought you would like to know how to loop through all sheets in a book.

# $source = $excel.Workbooks.Open($File)
#Loop through sheets
for ($i = 1; $i -le $source.Sheets.Count; $i++){
    $currentSheet = $source.Sheets($i)
    #Do something on $currentSheet
}

For the second question, I would do it like below.

# make a list for file you would like to look up
$files = @("File1.xlsx","File2.xlsx","File3.xlsx")
# loop through $files
for ($j = 0; $j -lt $files.Count; $j++){
    $FullPath = (Get-ChildItem -Path $files[$j]).FullName
    $source = $excel.Workbooks.Open($FullPath)
    #Do something on $source
}

I hope this helps.

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