[英]Inserting text from text file into existing Excel worksheet with PowerShell
我试图将我的文本文件的内容插入到Sheet1的A1单元格中,但是我得到的只是插入的文件名而不是文本文件的内容。
$Path = 'C:\folder\Test.xlsx'
$Text='C:\folder\text.txt'
# Open the Excel document and pull in the 'Play' worksheet
$Excel = New-Object -Com Excel.Application
$Excel.Visible=$true #For troubleshooting purposes only.
$Workbook = $Excel.Workbooks.Open($Path)
$page = 'Sheet1'
$ws = $Workbook.worksheets | where-object {$_.Name -eq $page}
# Set variables for the worksheet cells, and for navigation
$cells=$ws.Cells
$row=1
$col=1
$cells.item($Row,$col)=$Text
$col++
# Close the workbook and exit Excel
$workbook.Close($true)
$excel.quit()
那是因为您将$ Text设置为文件的路径。 您必须使用诸如Get-Content之类的cmdlet实际读取文件的内容。
例如:
$Text = Get-Content 'C:\folder\text.txt'
但是,根据该文本文件的内容,您可能想要以不同的方式进行操作,否则可能会导致混乱的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.