How to read Excel files in powershell?

2

I would like to parse the following Excel file which perfectly open in the application.

I browsed the Internet and lots of guys said that those two command lines do open Excel files (example Read Excel sheet in Powershell).

$excel = New-Object -com excel.application
$wb = $excel.workbooks.open("c:\users\administrator\my_test.xls")

But I have got the following exception:

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

How to outputs the value of each cell read-only, if possible keeping the rows ?

excel
powershell
asked on Stack Overflow Jan 25, 2018 by MUY Belgium • edited Jan 25, 2018 by Dirk Vollmar

1 Answer

1

As mentioned by commenters, the error you're seeing is due to your system having different regional settings to those of the document you want to edit. To get around the problem, you can change your system locale to that of the excel document in question. If, for instance, your document is of locale 'nl-BE' (assumption based on your username), you can do the following:

& {
    [threading.thread]::CurrentThread.CurrentCulture = 'nl-BE'
    $excel = New-Object -com excel.application
    $wb = $excel.workbooks.open("c:\users\administrator\my_test.xls")
  }

Hope this helps.

answered on Stack Overflow Jan 22, 2020 by borizzzzz

User contributions licensed under CC BY-SA 3.0