简体   繁体   English

没有Office套件的ComObject Excel.Application

[英]ComObject Excel.Application without Office Suite


I need to read an Excel file from Powershell. 我需要从Powershell读取一个Excel文件。
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: 它在具有Office安装的计算机上正常工作但如果未安装Office,则会收到以下错误:

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? 是否有一些Office使用的运行时环境

There's no "runtime" for Excel that you can use without obtaining a proper license for Excel (and installing Excel on the machine). 如果没有获得适当的Excel许可证(以及在机器上安装Excel),您可以使用Excel的“运行时”。 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). 如果您尝试在具有多个用户的服务器操作系统上执行此操作,则还需要考虑特殊许可(因为单个Excel许可证不会覆盖多个用户)。

You might consider using the OpenXML SDK for Office as a way of performing some common actions within an Excel file like documented here . 你可能会考虑使用的OpenXML SDK的办公室像记录的Excel文件中执行的一些常规操作的方式在这里 As it's a .NET library, you would be able to use it from within PowerShell. 因为它是一个.NET库,所以您可以在PowerShell中使用它。

No runtime, like Access. 没有运行时,比如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. 使用Excel Viewer,即使未安装Excel,也可以打开,查看和打印Excel工作簿。 You can also copy data from Excel Viewer to another program. 您还可以将Excel Viewer中的数据复制到另一个程序。 However, you cannot edit data, save a workbook, or create a new workbook. 但是,您无法编辑数据,保存工作簿或创建新工作簿。

Instead of COM it is possible to use Active X Data Objects (ADO) eg 而不是COM,可以使用Active X数据对象(ADO),例如

$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 资料来源: http//blogs.technet.com/b/heyscriptingguy/archive/2008/09/11/how-can-i-read-from-excel-without-using-excel.aspx

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

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