2016-08-12 9 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のようなコマンドレットでファイルの内容を読み取る必要があります。例えば

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

しかし、そのテキストファイルの内容に応じて、あなたは違ったことをやりたいか、あるいはあなたが厄介な結果に終わることができました。

関連する問題