Writing data to cells Excel 2007 / PowerShell

Why can't I write values ​​to Excel using Worksheet class or Sheet interface? I expected to be able to do something like this:

[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel")
$Excel = New-Object Microsoft.Office.Interop.Excel.ApplicationClass
$Workbook = $Excel.Workbooks.Add()
$Worksheet = $Workbook.Worksheets.Add()
$Worksheet.Cells.Item(1,1).Value2 = "Test"

      

But instead it seems that you need to write the values ​​using the ApplicationClass object:

[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel")
$Excel = New-Object Microsoft.Office.Interop.Excel.ApplicationClass
$Workbook = $Excel.Workbooks.Add()
$Worksheet = $Workbook.Worksheets.Add()
$Excel.Cells.Item(1,1).Value2 = "Test"

      

This doesn't seem logical to me because I am writing the value for the specific worksheet I am working with, not at the application level.

Any thoughts on this?

+2


source to share


1 answer


I cannot reproduce this. Your previous example works great for me. I checked by setting $ Excel.Visible = $ true and I can see "Test" in cell (1,1).



-Oisin

+3


source







All Articles