vba 如何使用powershell复制多个excel工作表并制作一个新的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23703354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:10:22  来源:igfitidea点击:

How to use powershell to copy several excel worksheets and make a new one?

excelvbapowershellautomationexcel-2010

提问by Matt A.

I have about 70 excel files I want to combine into a single document. Each document has only one sheet and follows this format:

我有大约 70 个要合并到一个文档中的 excel 文件。每个文档只有一张纸并遵循以下格式:

  • Row A with Columns A-F of headings
  • Row B with the first entry
  • Row C with the second entry
  • Up to 150 rows on some sheets
  • A 行,标题 AF 列
  • 第一个条目的 B 行
  • 带有第二个条目的 C 行
  • 某些工作表上最多 150 行

I want to scrape the information from Columns A-F for each row, and combine it into a new file with the information from all of the other files I have in the same directory.

我想从 AF 列中为每一行抓取信息,并将其与我在同一目录中的所有其他文件中的信息合并到一个新文件中。

Note: I only want to capture Columns A-F since in Column G there exists a Yes, No dataset to manage the drop down list in Column F.

注意:我只想捕获列 AF,因为在列 G 中存在一个是,否数据集来管理列 F 中的下拉列表。

I tried using dugan's answer from Copy Excel Worksheet from one Workbook to another with Powershellbut it resulted in a file with part of the data spread across two sheets.

我尝试使用杜根的回答从一个工作簿复制 Excel 工作表到另一个使用 Powershell,但它导致了一个文件,其中部分数据分布在两张工作表上。

Here is that code:

这是代码:

$file1 = 'C:\Users\Matthew.Andress\Documents\Excel Test\Book1.xlsx' # source's fullpath
$file2 = 'C:\Users\Matthew.Andress\Documents\Excel Test\Book2.xlsx' # destination's fullpath
$xl = new-object -c excel.application
$xl.displayAlerts = $false # don't prompt the user
$wb2 = $xl.workbooks.open($file1, $null, $true) # open source, readonly
$wb1 = $xl.workbooks.open($file2) # open target
$sh1_wb1 = $wb1.sheets.item(2) # second sheet in destination workbook
$sheetToCopy = $wb2.sheets.item('Sheet1') # source sheet to copy
$sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook
$wb2.close($false) # close source workbook w/o saving
$wb1.close($true) # close and save destination workbook
$xl.quit()
spps -n excel

Any suggestions? Thank you.

有什么建议?谢谢你。

回答by TheMadTechnician

Ok, it's a few days later, but you can thank the weekend for that. Take a look at this and see how you like it.

好的,这是几天后,但你可以感谢周末。看看这个,看看你喜欢它。

#Get a list of files to copy from
$Files = GCI 'C:\Users\Matt\Documents\Excel Test' | ?{$_.Extension -Match "xlsx?"} | select -ExpandProperty FullName

#Launch Excel, and make it do as its told (supress confirmations)
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $True
$Excel.DisplayAlerts = $False

#Open up a new workbook
$Dest = $Excel.Workbooks.Add()

#Loop through files, opening each, selecting the Used range, and only grabbing the first 6 columns of it. Then find next available row on the destination worksheet and paste the data
ForEach($File in $Files[0..4]){
    $Source = $Excel.Workbooks.Open($File,$true,$true)
    If(($Dest.ActiveSheet.UsedRange.Count -eq 1) -and ([String]::IsNullOrEmpty($Dest.ActiveSheet.Range("A1").Value2))){ #If there is only 1 used cell and it is blank select A1
        [void]$source.ActiveSheet.Range("A1","F$(($Source.ActiveSheet.UsedRange.Rows|Select -Last 1).Row)").Copy()
        [void]$Dest.Activate()
        [void]$Dest.ActiveSheet.Range("A1").Select()
    }Else{ #If there is data go to the next empty row and select Column A
        [void]$source.ActiveSheet.Range("A2","F$(($Source.ActiveSheet.UsedRange.Rows|Select -Last 1).Row)").Copy()
        [void]$Dest.Activate()
        [void]$Dest.ActiveSheet.Range("A$(($Dest.ActiveSheet.UsedRange.Rows|Select -last 1).row+1)").Select()
    }
    [void]$Dest.ActiveSheet.Paste()
    $Source.Close()
}
$Dest.SaveAs("C:\Users\Matt\Documents\Excel Test\Book1.xlsx",51)
$Dest.close()
$Excel.Quit()

That'll get a list of excel files, open Excel and create a new document, then cycle through the list of files, opening them, selecting Columns A-F, copying those columns, going back to the new workbook and selecting the next available row, and pasting the data from the other workbook. Then it closes that file and moves on to the next one. At the end it saves your workbook with all the data and closes excel.

这将获得 Excel 文件列表,打开 Excel 并创建一个新文档,然后循环浏览文件列表,打开它们,选择 AF 列,复制这些列,返回到新工作簿并选择下一个可用行,并粘贴其他工作簿中的数据。然后它关闭该文件并移至下一个文件。最后,它会保存包含所有数据的工作簿并关闭 Excel。