2010-07-12 4 views
2

Powershell을 사용하여 하나의 통합 문서에서 다른 통합 문서로 워크 시트를 복사 (또는 이동)하고 싶습니다.Powershell을 사용하여 하나의 통합 문서에서 다른 통합 문서로 Excel 워크 시트 복사

나는 이것을 전에 했었고 어떻게 기억하는지 안다. 나는 CopyTo() 함수를 사용했다고 생각한다.

그냥 사용해보십시오.

$missing = [System.Type]::missing 
$excel = New-Object -Com Excel.Application 

$wb1 = $excel.Workbooks.Add($missing) 
$wb2 = $excel.Workbooks.Add($missing) 

# Now, to copy worksheet "Sheet3" from $wb2 into $wb1 as second worksheet. 
# How? 

답변

7

두 번째 시트에 복사 Kiron

변경된 인덱스

에서 post를 참조하십시오

$file1 = 'C:\Users\eric\Documents\Book1.xlsx' # source's fullpath 
$file2 = 'C:\Users\eric\Documents\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('Sheet3') # 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 
+0

와우, 감사합니다 .. 추가 정보의 많은 내 코드를 개선 할 수 있습니다. – idazuwaika

+0

+1 원래 포스터에 공헌을 한 경우! – technomalogical

+0

내가 가진 한 가지 문제를 제외하고는 모두 정상적으로 작동합니다. 복사가 끝나면 대상 시트는 복사 된 파트에 대해 선택된 상태로 유지됩니다. 어떻게 비활성화합니까? – Rudra

관련 문제