Powershell如何复制()工作表并保存它自己的工作簿

我并不擅长使用PowerShell,但是我正在编写一个脚本,用于打开Excel文件,从.txt文件中读取内容,然后将这些数据导入到Excel表格中。 input的数据将赋予另一个工作表上的图表值。 我想询问用户他们是否想创build这个图表的副本(不总是需要)如果用户想然后它将只复制图表工作表,并保存自己的工作簿(..copyedChart.xls )。

现在,我知道什么时候使用.Copy()函数,它将采取当前活动工作表,并已经打开了一个新的Excel实例。 我的问题实际上是能够“控制”这个新的Excel实例,我无法理解如何实际调用该工作表并保存它。

这是我所拥有的..

#Create an instance of Excel $excel = New-Object -comobject Excel.Application Write-Host "Initializing applications.." #all Write-Host are for the users.. #declaring sheet names.. $sheetName = "Sheet1" $sheetName2 = "Sheet2" $excel.displayAlerts = $false Try{ #open Excel file [string]$file = "C:\Users\Desktop\test.xls" #create a reference to the specified excel workbook $workBook = $excel.Workbooks.open($file) #activates sheet $sheet = $workBook.WorkSheets.Item($sheetName).activate() $excel.Visible = $true $data = Get-Content C:\Users\Desktop\input.txt }#end try Catch{ Write-Host "An error has occured." }#end catch Write-Host "Inputting new data.." $i = 0 $rowNumber = 2 #Im just parsing the input.txt and spitting it out into excel foreach($row in $data){ if($row){ [Array] $index = $row.Split(" ") $i++ $column = 1 if($i -ge 1){ foreach($item in $index){ $excel.Cells.Item($rowNumber, $column) = "$item" $column++ }#end foreach $rowNumber++ }#end if $i }#end if $row }#end foreach $date = $excel.Cells.Item(2, 1).Value().toString("MMMM-dd-yyyy") #row, column #changes the active sheet $sheet2 = $workBook.Sheets.Item($sheetName2).Select() #references the active chart on the active page $chart = $workBook.ActiveChart Write-Host "Updating charts.." #changes the title of the chart to include the current date $chart.ChartTitle.Text = "Some title - $date" #saves the files to these locations $save = 'C:\Users\Desktop\'+$date+'-text.xls' $saveChartCopy = 'C:\Users\Desktop\'+$date+'-CHARTCOPY.xls' Write-Host "Saving new file.." #save this workbook $workBook.SaveAs($save) #asks the user if they would like to create a copy of the chart $makeCopy = Read-Host ("Would you like to create a copy of the chart? (y/n)") #-----------------------------------------------------------STUCK HERE #if yes, copy and save the chart as a new workbook. if($makeCopy -eq "y" -or $makeCopy -eq "Y"){ $copiedChart = $chart.Copy() #Copies the chart and opens into a new instance of excel... $copiedChart.SaveAs($saveChartCopy) #My sad attempt at trying to save the copied chart... } #if no, than close excel elseif($makeCopy -eq "n" -or $makeCopy -eq "N"){ #close excel Write-Host "Closing Excel.." $excel.Quit() Write-Host "Complete!" } else{ Read-Host "Please enter a valid option!" } 

如果对我所问的问题有任何疑惑,请问,我会尽量进一步解释。

另外,因为我是新来的PowerShell,我是一种菜鸟程序员..我打开所有其他input关于我的代码。

您必须使用$ Excel.Workbooks.add()创build一个新的工作簿对象:

 $NewWorkBook = $Excel.Workbooks.Add() # Copy and paste your sheet $NewWorkBook.SaveAs($FileName) $NewWorkBook.Close() 

如果我正确地理解了你的问题(请告诉我,如果我不是),你所要做的就是在另一个variables中抓住新的工作簿的句柄。 我很好奇自己,并决定抛出一些快速和肮脏的东西来玩弄它。 尝试这样的事情:

 $Excel = New-Object -ComObject "Excel.Application" $Workbook = $Excel.Workbooks.Add() $Sheet = $Workbook.Worksheets.Item(1) $Excel.Visible = $true $Cells = $Sheet.Cells $Workbook2 = $Excel.Workbooks.Add() $Sheet2 = $Workbook2.Worksheets.Item(1) $Cells2 = $Sheet2.Cells $Cells.Item(1,1) = "Book 1" $Cells2.Item(1,1) = "Book 2" $Workbook.Close() $Workbook2.Close() $Excel.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) 

另外,不要忘记清理ComObject!