'ComObject Excel.Application without Office Suite


I need to read an Excel file from Powershell.
I am using this object:

$objExcel=New-Object -ComObject Excel.Application

It is working fine on a machine with an Office installation but if Office is not installed, I get this error:

Retrieving the COM class factory for component with CLSID {} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Is there some runtime environment for Office to use?



Solution 1:[1]

There's no "runtime" for Excel that you can use without obtaining a proper license for Excel (and installing Excel on the machine). If you were trying to do this on a Server OS with multiple users, you'd need to consider special licensing there too (as a single Excel license wouldn't cover multiple users more than likely).

You might consider using the OpenXML SDK for Office as a way of performing some common actions within an Excel file like documented here. As it's a .NET library, you would be able to use it from within PowerShell.

Solution 2:[2]

Instead of COM it is possible to use Active X Data Objects (ADO) e.g.

$strFileName = "C:\Data\scriptingGuys\Servers.xls"
$strSheetName = 'ServerList$'
$strProvider = "Provider=Microsoft.Jet.OLEDB.4.0"
$strDataSource = "Data Source = $strFileName"
$strExtend = "Extended Properties=Excel 8.0"
$strQuery = "Select * from [$strSheetName]"

$objConn = New-Object System.Data.OleDb.OleDbConnection("$strProvider;$strDataSource;$strExtend")
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand($strQuery)
$sqlCommand.Connection = $objConn
$objConn.open()

$DataReader = $sqlCommand.ExecuteReader()

While($DataReader.read())
{
 $ComputerName = $DataReader[0].Tostring() 
 "Querying $computerName ..."
 Get-WmiObject -Class Win32_Bios -computername $ComputerName
}  
$dataReader.close()
$objConn.close()

Source: http://blogs.technet.com/b/heyscriptingguy/archive/2008/09/11/how-can-i-read-from-excel-without-using-excel.aspx

Solution 3:[3]

Warning: Excel Viewer is retired.

No runtime, like Access.

There is a viewer, if that's any help.

Overview

With Excel Viewer, you can open, view, and print Excel workbooks, even if you don't have Excel installed. You can also copy data from Excel Viewer to another program. However, you cannot edit data, save a workbook, or create a new workbook.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WiredPrairie
Solution 2 flux
Solution 3 Bender the Greatest