我正在尝试将文本文件的内容插入到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'
但是,根据该文本文件的内容,您可能希望以不同的方式执行此操作,否则可能会导致混乱的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句