问题描述
我想打开一个excel工作簿并读出数据,进行其他类型的操作,等等.我知道我必须添加一个程序集引用:
I want to open an excel workbook and read out data, do other kinds of operations, etc. I know that I have to add an assembly reference:
[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft Office\Office16\ADDINS\Microsoft Power Query for Excel Integrated\bin\Microsoft.Office.Interop.Excel.dll")
然后我需要实例化一个Application对象.
And then I need to instantiate an Application object.
$workbook = New-Object -TypeName Microsoft.Office.Interop.Excel.Application
这将返回错误找不到构造函数"是不是Microsoft.Office.Interop.Excel.Application实际上是一个接口?我想知道如何在此场景中实例化它.
This however returns an error "A constructor was not found"Isn't by the way Microsoft.Office.Interop.Excel.Application an interface actually? I am wondering how it can be instantiated in this scenario.
推荐答案
您需要将其作为ComObject打开.
You need to open it as a ComObject.
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($FilePath)
在该示例中,您需要将$FilePath
定义为要打开的Excel文件的完整路径.
In that example you would have needed to define $FilePath
as the full path to the Excel file that you are trying to open.
这篇关于如何从Powershell打开Excel工作簿以实现自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!