简体   繁体   English

如何在powershell中读取Excel文件?

[英]How to read Excel files in powershell?

I would like to parse the following Excel file which perfectly open in the application.我想解析以下在应用程序中完美打开的 Excel 文件。

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 文件(例如在 Powershell 中读取 Excel 表)。

$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))"使用“1”参数调用“Open”的异常:“旧格式或无效类型库。(来自 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.要解决此问题,您可以将系统区域设置更改为相关 excel 文档的区域设置。 If, for instance, your document is of locale 'nl-BE' (assumption based on your username), you can do the following:例如,如果您的文档是语言环境“nl-BE”(基于您的用户名的假设),您可以执行以下操作:

& {
    [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.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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