为了解释,我目前有90个不同的文件,我需要在处理之后,将文件的前两个选项卡分开并将粘贴复制到新工作簿并保存该工作簿,其中包含报告人员的姓名将转到(名称在每个文件中)并通过电子邮件发送出去。
我需要2件事的帮助。我无法使用单元格作为名称的参考来保存文件,并同时复制两(2)张(sheet1依赖于sheet2,复制/粘贴分别断开连接)。
任何人都可以帮忙吗?
更新: 我已经能够将保存工作:),但仍然需要帮助才能同时复制两个工作表。
这是我的更新代码:
$Files = GCI '\\networkfolder\workingfolder' | ?{$_.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
#Loop through files and perform tasks
ForEach($File in $Files[0..4]){
$Source = $Excel.Workbooks.Open($File,$true,$true) #Open's source
$NewWorkBook = $Excel.Workbooks.Add() # open target (New Workbook)
$sh1_wb1 = $NewWorkBook.sheets.item(1) # first sheet in destination workbook
$sheetToCopy2 = $Source.sheets.item(2) # Alias for 2nd Sheet
$sheetToCopy = $Source.sheets.item(1) # Alias for 1st Sheet
$SheetsToCopy = ($sheetToCopy, $sheetToCopy2) # Selects sheets 1 and 2
#I think the fix to copy both sheets at the same time would be in this step
$SheetsToCopy.copy($sh1_wb1) # copy source sheet 1 & 2 to destination workbook
$ws1 = $NewWorkBook.worksheets | where {$_.name -eq "Charts Data Tab"} #Selects Sheet2
$ws2 = $NewWorkBook.worksheets | where {$_.name -eq "Dashboard"} #Selects Sheet1
$range = $ws1.Rows.Item("1:5000")
[void]$range.select()
$range.Copy();
$range.PasteSpecial(-4163)
$Source.close($false) # close source workbook w/o saving
$Name = $ws1.Cells.Item.Invoke(2,4).Value2 #Registers contents of D2 of Sheet2
$FName = "\\networkfolder\Testing\$Name.xlsx"
$NewWorkBook.SaveAs($FName)
Start-Sleep 50000
}
$xl.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
答案 0 :(得分:0)
您应该查看PSExcel库。它提供了许多使用Excel的功能。