2014-04-30 33 views
3


我需要從Powershell讀取Excel文件。
我使用這個對象:
ComObject Excel.Application without Office Suite

$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使用?

回答

1

沒有運行時,像Access。

有一個查看器,如果有任何幫助。

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.

+1

謝謝,但與瀏覽器沒有運氣,同樣的錯誤 –

3

沒有Excel的「運行時」,您可以在沒有獲得適當的Excel許可證的情況下使用Excel(並在機器上安裝Excel)。 如果您嘗試在具有多個用戶的服務器操作系統上執行此操作,則需要考慮特殊許可(因爲單個Excel許可證不會覆蓋多個用戶)。

您可能會考慮使用OpenXML SDK for Office作爲在文件here等Excel文件中執行一些常用操作的方式。因爲它是一個.NET庫,您可以在PowerShell中使用它。

1

而不是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() 

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

相關問題