【问题标题】:ComObject Excel.Application without Office SuiteComObject Excel.Application 没有 Office 套件
【发布时间】:2014-06-16 20:41:16
【问题描述】:


我需要从 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 powershell ms-office powershell-2.0


    【解决方案1】:

    在没有获得适当的 Excel 许可证(并在计算机上安装 Excel)的情况下,您可以使用 Excel 的“运行时”。 如果您尝试在具有多个用户的服务器操作系统上执行此操作,则还需要考虑那里的特殊许可(因为单个 Excel 许可不太可能涵盖多个用户)。

    您可以考虑使用OpenXML SDK for Office 作为在 Excel 文件中执行一些常见操作的一种方式,例如记录在案的here。由于它是一个 .NET 库,因此您可以在 PowerShell 中使用它。

    【讨论】:

      【解决方案2】:

      可以使用 Active X 数据对象 (ADO) 代替 COM,例如

      $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

      【讨论】:

        【解决方案3】:

        警告: Excel Viewer 已停用。

        没有运行时,如 Access。

        有一个查看器,如果有帮助的话。

        概述

        使用 Excel Viewer,您可以打开、查看和打印 Excel 工作簿,即使您没有安装 Excel。你也可以复制 Excel Viewer 中的数据到另一个程序。但是,您无法编辑 数据、保存工作簿或创建新工作簿。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-18
          • 2012-01-09
          • 2019-08-23
          • 2019-08-25
          • 1970-01-01
          • 2023-04-09
          • 2021-06-11
          相关资源
          最近更新 更多