我需要从Powershell中读取一个Excel文件
我正在使用此对象:
$objExcel=New-Object -ComObject Excel.Application
它在安装了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)).
是否有一些运行时环境可供Office使用?
如果没有适当的Excel许可证(并在机器上安装Excel(,就无法使用Excel的"运行时">如果你试图在有多个用户的服务器操作系统上执行此操作,你也需要考虑特殊许可(因为单个Excel许可证很可能无法覆盖多个用户(
您可以考虑使用OpenXMLSDKforOffice作为在Excel文件中执行一些常见操作的一种方式,如本文所述。因为它是一个。NET库,您可以在PowerShell中使用它。
可以使用Active X数据对象(ADO(代替COM,例如
$strFileName = "C:DatascriptingGuysServers.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()
来源:http://blogs.technet.com/b/heyscriptingguy/archive/2008/09/11/how-can-i-read-from-excel-without-using-excel.aspx
警告:Excel Viewer已停用。
没有运行时,就像Access一样。
如果有帮助的话,还有一个观众。
概述
使用Excel Viewer,即使您没有安装Excel,也可以打开、查看和打印Excel工作簿。您也可以复制数据从Excel Viewer传输到另一个程序。但是,您不能编辑数据、保存工作簿或创建新工作簿。