$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"} #<——- Selects sheet 1
$ws2 = $wb.worksheets | where {$_.name -eq "sheet2"} #<——- Selects sheet 2
$ws3 = $wb.worksheets | where {$_.name -eq "sheet3"} #<——- Selects sheet 3
# 激活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")
如何为Excel 工作薄设置数据有效性验证
$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
添加图表到 Excel 工作薄
$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 # <– use the one you like
对Excel 工作薄的某一列进行排序
$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)
对Excel的某一列进行汇总
\ | 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)