powershell 조작 excel
53275 단어 2018 가을powershell
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Add()
$xl = new-object -comobject excel.application
$xl.Visible = $true
# ado_object_models.xls Excel
$wb = $xl.Workbooks.Open("C:\Scripts\ado_object_model.xls")
#
$ws = $xl.Sheets.Add()
# Excel
$xl = new-object -comobject Excel.Application
# Excel
$xl.visible = $true
# Excel
$wb = $xl.workbooks.open("C:\Scripts\PowerShell\test.xls")
# Excel
$ws1 = $wb.worksheets | where {$_.name -eq "sheet1"} #
$ws2 = $wb.worksheets | where {$_.name -eq "sheet2"} #
$ws3 = $wb.worksheets | where {$_.name -eq "sheet3"} #
# sheet1
$ws1.activate()
Start-Sleep 1
# sheet 2
$ws2.activate()
Start-Sleep 1
# sheet 3
$ws3.activate()
$xl.ActiveCell.Value2 = "x"
$xl.activesheet.cells.item(2,1).value2=”y”
$xl.cells.item(2,1).value2=”y”
$xl.ActiveSheet.Range("B1").Value2 = "y"
$wb.Name
$xl.activeWorkBook.name
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
# 11 , 1–〉11
for ($row = 1; $row -lt 11; $row++)
{
$ws.Cells.Item($row,1) = $row
}
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$row = 1
#
$s = dir
$s | foreach -process `
{ `
# Excel
$ws.Cells.Item($row,1) = $_; `
$row++ `
}
# Excel , Excel quit()
# , Excel , Excel
# , , Com ,
# .net 。 office Powershell
# office application 。
function Release-Ref ($ref) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ref)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# —————————————————–
$xl = New-Object -comobject Excel.Application
$xl.Visible = $True
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$range = $ws.Cells.Item(1,1)
$row = 1
# ,
$s = Get-Process | Select-Object name
$s | foreach -process {
$range = $ws.Cells.Item($row,1);
$range.value2 = $_.Name;
$row++ }
# office ,
$xl.DisplayAlerts = $False
# excel Saveas
$wb.SaveAs("C:\Scripts\Get_Process.xls")
# $range
Release-Ref $range
# powershell blog , remove-variable $range,
# , ,
# variable:\ , dir variable:\
remove-variable $range
Release-Ref $ws
remove-variable $ws
Release-Ref $wb
remove-variable $wb
$xl.Quit()
Release-Ref $xl
remove-variable $xl
$strComputer = (remote machine name)
$P = gwmi win32_process -comp $strComputer
#an existing Workbook
$xl.Workbooks.Open("C:\Scripts\ado_object_model.xls")
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Add()
$xl.ActiveCell.Value2 = "x"
$xl.ActiveSheet.Range("B1").Value2 = "y"
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]
$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# —————————————————–
$xl = New-Object -comobject Excel.Application
$xl.Visible = $True
$wb = $excel.Workbooks.Add()
$ws = $workbook.Worksheets.Item(1)
$range = $worksheet.Cells.Item(1,1)
$row = 1
$s = Get-History | foreach -process { `
$range = $worksheet.Cells.Item($row,1); `
# Value2 , value 。
$range.value2 = $_.CommandLine; `
$row++ }
$xl.DisplayAlerts = $False
$wb.SaveAs("C:\Scripts\Get_CommandLine.xls")
Release-Ref $range
Release-Ref $ws
Release-Ref $wb
$xl.Quit()
Release-Ref $xl
# Script name: ConvertTilde.ps1
# Created on: 2007-01-06
# Author: Kent Finkle
# Purpose: How Can I Convert a Tilde-Delimited File to Microsoft Excel
Format?
$s = gc C:\Scripts\Test.txt
# ~ Tab , Excel ,
# import-csv CSV excel
$s = $s -replace("~","`t")
$s | sc C:\Scripts\Test.txt
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.Workbooks.Open("C:\Scripts\Test.txt")
$comments = @’
Script name: Add-Validation.ps1
Created on: Wednesday, September 19, 2007
Author: Kent Finkle
Purpose: How can I use Windows Powershell to Add Validation to an
Excel Worksheet?
‘@
#—————————————————–
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
#—————————————————–
$xlValidateWholeNumber = 1
$xlValidAlertStop = 1
$xlBetween = 1
$xl = new-object -comobject excel.application
$xl.Visible = $True
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$r = $ws.Range("e5")
$r.Validation.Add($xlValidateWholeNumber, ` $xlValidAlertStop,
$xlBetween, "5", "10") $r.Validation.InputTitle = "Integers"
$r.Validation.ErrorTitle = "Integers"
$r.Validation.InputMessage = "Enter an integer from five to ten"
$r.Validation.ErrorMessage = "You must enter a number from five to
ten"
$a = Release-Ref $r
$a = Release-Ref $ws
$a = Release-Ref $wb
$a = Release-Ref $xl
$xrow = 1
$yrow = 8
$xl = New-Object -c excel.application
$xl.visible = $true
$wb = $xl.workbooks.add()
$sh = $wb.sheets.item(1)
1..8 | % { $sh.Cells.Item(1,$_) = $_ }
1..8 | % { $sh.Cells.Item(2,$_) = 9-$_ }
$range = $sh.range("a${xrow}:h$yrow")
$range.activate
# create and assign the chart to a variable
$ch = $xl.charts.add()
$ch.chartType = 58
$ch.setSourceData($range)
$ch.export("C:\test.jpg")
$xl.quit()
# excel has 48 chart styles, you can cycle through all
1..48 | % {$ch.chartStyle = $_; $xl.speech.speak("Style $_"); sleep 1}
$ch.chartStyle = 27 #
$xlSummaryAbove = 0
$xlSortValues = $xlPinYin = 1
$xlAscending = 1
$xlDescending = 2
# one-column sort –> works
[void]$range1.sort($range2, $xlAscending)
[void]$range1.sort($range3, $xlAscending)
# two-column sort –> doesn’t sort both columns
# the 4th arg [xlSortType] gives problems so pass $null or ”,
# it may be for PivotTables only
[void]$range1.sort($range2, $xlAscending, $range3, ”, $xlAscending)
\
mon
tue
wed
eggs
1
1
1
ham
5
5
5
spam
1
4
7
spam
2
5
8
spam
3
6
9
처리 코드:
$xlSum = -4157
$range = $xl.range("A1:D6")
$range.Subtotal(1,-4157,(2,3,4),$true,$false,$true)
# Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4), _
# Replace:=True, PageBreaks:=False, SummaryBelowData:=True
$xlAutomatic=-4105
$xlBottom = -4107
$xlCenter = -4108
$xlContext = -5002
$xlContinuous=1
$xlDiagonalDown=5
$xlDiagonalUp=6
$xlEdgeBottom=9
$xlEdgeLeft=7
$xlEdgeRight=10
$xlEdgeTop=8
$xlInsideHorizontal=12
$xlInsideVertical=11
$xlNone=-4142
$xlThin=2
$xl = new-object -com excel.application
$xl.visible=$true
$wb = $xl.workbooks.open("d:\book1.xls")
$ws = $wb.worksheets | where {$_.name -eq "sheet1"}
$selection = $ws.range("A1:D1")
$selection.select()
$selection.HorizontalAlignment = $xlCenter
$selection.VerticalAlignment = $xlBottom
$selection.WrapText = $false
$selection.Orientation = 0
$selection.AddIndent = $false
$selection.IndentLevel = 0
$selection.ShrinkToFit = $false
$selection.ReadingOrder = $xlContext
$selection.MergeCells = $false
$selection.Borders.Item($xlInsideHorizontal).Weight = $xlThin
$xlFillWeekdays = 6
$xl = new-object -com excel.application
$xl.visible=$true
$wb = $xl.workbooks.add()
$ws = $wb.worksheets | where {$_.name -eq "sheet1"}
$range1= $ws.range("A1")
$range1.value() = (get-date).toString("d")
$range2 = $ws.range("A1:A25")
$range1.AutoFill($range2,$xlFillWeekdays)
$range1.entireColumn.Autofit()
# $wb.close()
# $xl.quit()
# get-excelrange.ps1
# opens an existing workbook in Excel 2007, using PowerShell
# and turns a range bold # Thomas Lee – t…@psp.co.uk
# Create base object
$xl = new-object -comobject Excel.Application
# make Excel visible
$xl.visible = $true
# open a workbook
$wb = $xl.workbooks.open("C:\Scripts\test.xls")
# Get sheet1
$ws = $wb.worksheets | where {$_.name -eq "sheet1"}
# Make A1-B1 bold
$range = $ws.range("A1:B1")
$range.font.bold = "true"
# Make A2-B2 italic
$range2 = $ws.range("A2:B2")
$range2.font.italic = "true"
# Set range to a value
$range3=$ws.range("A2:B2")
$Range3.font.size=24
# now format an entire row
$range4=$ws.range("3:3")
$range4.cells="Row 3"
$range4.font.italic="$true"
$range4.font.bold=$True
$range4.font.size=10
$range4.font.name="comic Sans MS"
# now format a Range of cells
$ws.Range("D1:F5").NumberFormat = "#,##0.00"
$xll = New-Object -com Excel.Application
$xl.visible = $True
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$ws.Cells.Item(1,1) = “A value in cell A1.”
[void]$ws.Range("A1").AddComment()
[void]$ws.Range("A1").comment.Visible = $False
[void]$ws.Range("A1").Comment.text("OldDog: `r this is a comment")
[void]$ws.Range("A2").Select
$xlPasteValues = -4163
$xlCellTypeLastCell = 11
$used = $ws.usedRange
$lastCell = $used.SpecialCells($xlCellTypeLastCell)
$row = $lastCell.row
$range = $ws.UsedRange
[void]$ws.Range("A8:F$row").Copy()
[void]$ws.Range("A8").PasteSpecial(-4163)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
확장-JsonFileJson 형식은 사물을 정의하는 놀라운 방법이므로 개발자는 엔티티를 설명하기 위해 코드 저장소에 Json 형식을 자주 사용합니다. Devops는 또한 json 파일을 사용하여 애플리케이션 또는 기타 항목을 구성합니다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.