PowerShell 技能连载 - 将值保存到 Excel 工作表中

有些时候,您可能会需要更新一个 Excel 工作表中的值。PowerShell 可以操作 Excel 对象模型,不过它很慢。以下是一个打开 Excel 文件,然后写入信息到 A1 单元格,最后保存更改的例子,

请确保您调整了路径,指向一个实际存在的 Excel 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$excel = New-Object -ComObject Excel.Application
# open Excel file
$workbook = $excel.Workbooks.Open("c:\test\excelfile.xlsx")

# uncomment next line to make Excel visible
#$excel.Visible = $true

$sheet = $workbook.ActiveSheet
$column = 1
$row = 1
# change content of Excel cell
$sheet.cells.Item($column,$row) = Get-Random
# save changes
$workbook.Save()
$excel.Quit()

PowerShell 技能连载 - 将值保存到 Excel 工作表中

https://blog.vichamp.com/2018/03/01/saving-values-to-excel-sheet/

作者

Victor Woo

发布于

2018-03-01

更新于

2018-03-01

许可协议

PowerShell 技术 QQ 群