2016-08-12 33 views
0

我试图将我的文本文件的内容插入Sheet1上的单元格A1,但我所获得的是插入的文件名而不是文本文件的内容。使用PowerShell将文本从文本文件插入到现有的Excel工作表中

$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() 

回答

0

这是因为您将$ Text设置为文件的路径。您必须使用像Get-Content这样的cmdlet实际读取文件的内容。

例如:

$Text = Get-Content 'C:\folder\text.txt' 

但是,根据该文本文件的内容,你可能想要做的不同,或者你可以用一个混乱的结果告终。

相关问题