簡體   English   中英

如何在 Linux 上的 PowerShell 中讀取 Excel 文件?

[英]How to read an Excel file in PowerShell on Linux?

許多人都知道,PowerShell 現在可以在 Linux 上安裝和使用,到目前為止,它對我來說工作得非常好。 但是,鑒於無法在 Linux 上安裝 Excel,我想知道是否可以讀取 Excel 文件的內容。 我在網上找到的是打開 Excel 文件的 PS 腳本是:

# Script copied and pasted from https://stackoverflow.com/questions/48443536/how-to-read-excel-files-in-powershell

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

但我得到了錯誤:

New-Object: A parameter cannot be found that matches parameter name 'com'.

當我刪除參數並運行腳本時,這是我得到的錯誤:

New-Object: Cannot find type [excel.application]: verify that the assembly containing this type is loaded.

Since I'm on Linux (ubuntu 22.04), I can use xls2csv on bash to convert the Excel file to csv and then use Get-Content on PowerShell. 但我想知道是否有辦法在 PowerShell 上做到這一點,因為我目前正在學習它。

謝謝你。

我感謝@Olaf 指出了 Doug Finke 的ImportExcel模塊。

安裝模塊后:

Install-Module -Name ImportExcel

導入文件很簡單:

$data = Import-Excel c:\users\administrator\my_test.xls

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM