Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 169668

Import-xlsx with powershell

$
0
0

Import-CSV in all glory, but sometimes it's just not enough... I'd really like to read a xls- or xlsx-file to be able to automate even more.

 

I've found that this pretty much should do the trick:

$xl = New-Object -COM "Excel.Application"
$xl.Visible = $true
$wb = $xl.Workbooks.Open("C:\path\to\your.xlsx")
$ws = $wb.Sheets.Item(1)

 

However, this results in an error:

Exception calling "Open" with "1" argument(s): "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"

At 1:3 char:1

+ $wb = $xl.Workbooks.Open("C:\path\to\your.xlsx")

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ComMethodTargetInvocation

 

Some research into this reveals that this is due to not having EN-us as regional setting (or not using default formatting in Excel), an old powershell 1.0 bug that still ( !) doesn't seem to be resolved.

KB 320369

 

The KB mentions a few work arounds, but these also result in errors. Also I'd like the script to be able to run regardless regional settings.

After looking further into this I found a blog that should fix the work around, but I still get error:

Exception calling "InvokeMember" with "6" argument(s): "Object does not match target type."

At 1:3 char:2

+     $m.PSBase.GetType().InvokeMember(

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : TargetException

 

I also found this, what seems to be a awesome, script by Josh Miller who creates an Import-Excel function. This gives me the same error as the first one.

 

Does anyone know of a fix for this, a working work-around, or if there is a new built in function for powershell 3?

 

I'm using powershell 3.0, powercli 5.1, Excel 2013 is installed.


Viewing all articles
Browse latest Browse all 169668

Trending Articles