简体   繁体   中英

How to read Excel files in powershell?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM