以下是我的代码:
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $true
$objExcel.SheetsInNewWorkbook = 1
$objWorkBook = $objExcel.Workbooks.Add()
$objWorkSheet = $objWorkBook.Sheets.Item(1)
$objWorkSheet.Activate()
$strHome = "C:\share"
$strAccessFile = $strHome + "\db1.accdb"
$strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$strAccessFile;Persist Security Info=True;"
$adOpenStatic = 3
$adLockOptimistic = 3
$objConnection = New-Object -ComObject ADODB.Connection
$objConnection.Open($strConnectionString)
$objRecordSet = New-Object -ComObject ADODB.RecordSet
$strAccessString = "SELECT * FROM Table"
$objRecordSet.Open($strAccessString,$objConnection,$adOpenStatic,$adLockOptimistic)
$objRecordSet.Fields.Item("Text").Value
$oRange = $objWorkSheet.Range("B4")
$oRez = $objWorkSheet.QueryTables.Add($objRecordSet,$oRange)
Excel已打开,与Access记录集相同。 Field" Text"显示了价值。
最后一行代码产生错误:
Exception calling "Add" with "2" argument(s): "Parameter is not correct. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))"
At C:\share\dataconn_test.ps1:26 char:1
+ $oRez = $objWorkSheet.QueryTables.Add($objRecordSet,$oRange)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
为什么?我做错了什么?