我希望获取在PowerShell中生成的字符串,并将该字符串粘贴\返回到打开的电子表格的单元格A:1中。我对此进行了研究,但我不理解excel的特定语法。我需要为正在运行的特定Excel工作簿标识和声明PID吗?如果需要,如何标识和声明PID?
我知道如何创建新的工作簿并将文本添加到单元格A:1,但不是已经打开的单元格。粘贴后,它还必须“按回车”来执行电子表格函数。
我不知道从哪里开始,除了:(new-object -com "excel.application").Workbooks.Add()).application.Visible=$True
我已经在网上搜索过了,但没有找到任何有意义的例子。请帮帮忙
发布于 2019-07-22 07:38:19
您可以使用以下脚本来实现此目的
#Specify the path of the excel file
$FilePath = "path\test.xlsx"
#Specify the Sheet name
$SheetName = "Sheet1"
# Create an Object Excel.Application using Com interface
$objExcel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
$WorkSheet.Cells.Item(1,1) = "Link" #Updates the first cell (A1)
$WorkBook.Save()
$WorkBook.Close()
确保该文件具有来自PowerShell脚本的写入权限。
如果要编辑已打开的工作表,请更改文件的路径并正确提及工作表名称,它将按预期工作。
https://stackoverflow.com/questions/57139906
复制相似问题