简体   繁体   中英

How to read an Excel file in PowerShell on Linux?

As many of you are aware, PowerShell can now be installed and used on Linux and it has worked surprisingly well for me so far. However, I wonder if the content of an Excel file can be read given that Excel cannot be installed on Linux. What I have found online is that the PS script to open an Excel file is:

# 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")

But I get the error:

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

When I remove the parameter and run the script, this is the error I get:

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. But I would like to know if there is a way to do it on PowerShell since I am currently learning it.

Thank you.

I am grateful to @Olaf who pointed out the ImportExcel module from Doug Finke.

After installing the module with:

Install-Module -Name ImportExcel

importing the file is as easy as:

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

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