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

时间:2014-01-28 19:07:07

标签: excel powershell powershell-ise

我对PowerShell不太满意,但我正在编写一个脚本,打开一个excel文件,从.txt文件中读取内容,并将该数据抛出到excel表中。输入的数据将为另一个工作表上的图表提供值。我想询问用户是否要创建此图表的副本(并非总是需要)如果用户希望它将仅复制图表工作表,并保存其自己的工作簿(..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的新手,而且我是一名noob程序员。我对所有其他关于我的代码的输入都持开放态度。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

如果我理解你的问题(请告诉我,如果我不是),你所要做的就是在另一个变量中捕获新工作簿的句柄。我自己很好奇,并决定扔掉一些快速而肮脏的东西来玩它。尝试这样的事情:

$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!