How to access this object in Powershell?

Feb 8, 2013 at 5:00 PM
Hello,

I'm trying to use this library in Powershell on Windows 7/Server2008R2.
Do you have any examples of using it? If so how do I create a object from it?

Also, I had to change the targetFramework to .net 3.5 so it would load in Powershell.

$filepath="C:\Users\me\Downloads\2.1.beta.src\Excel\bin\Debug\Excel.4.0.dll"
System.Reflection.Assembly::LoadFile($filepath)
$a = New-Object [Excel.ExcelBinaryReader]

Output:

PS C:\Windows\system32> $filepath="C:\Users\me\Downloads\2.1.beta.src\Excel\bin\Debug\Excel.4.0.dll"

System.Reflection.Assembly::LoadFile($filepath)

$a = New-Object [Excel.ExcelBinaryReader]

New-Object -TypeName [Excel.4.0].IExcelDataReader

[appdomain]::CurrentDomain.GetAssemblies()

GAC Version Location

False v2.0.50727 C:\Users\me\Downloads\2.1.beta.src\Excel\bin\Debug\Excel.4.0.dll
New-Object : Cannot find type [[Excel.ExcelBinaryReader]]: make sure the assembly containing this type is loaded.
At line:5 char:16
  • $a = New-Object <<<< [Excel.ExcelBinaryReader]
    • CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
    • FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Developer
Feb 9, 2013 at 12:36 AM
The constructor for ExcelBinaryReader is internal. There's code for how to use the project on the project home page. Load the assembly as you did, then get a FileStream:
> $file = [System.IO.File]::OpenRead("$($pwd.Path)\Book1.xls")
Pass the FileStream to the factory method:
> $reader = [Excel.ExcelReaderFactory]::CreateBinaryReader($file)
And read from it as directed on the home page. You should make sure to close and dispose the reader and file when you're done with them.