无法使用Interopservices

时间:2017-08-07 22:23:13

标签: excel powershell

我试图在SQL导入脚本之后关闭excel,但我无法使用Interopservices方法释放Com对象。

当我尝试使用时释放com对象 [System.Runtime.Interopservices.Marshal]::ReleaseComObject(New-Object -Com Excel.Application) 我收到一个错误,我错过了一个括号。 我已经在其他人的代码中回顾了这个过程的一些例子,虽然我必须遗漏一些东西,但我仍然缺少这些东西,并且在找到它时需要一些帮助。

以下是我的脚本。

$serverName = "SERVER";
$databaseName = "User_Data" ;
$tableName = "TABLE" ;
$filepath = "Path to Xls";

#create object to open Excel workbook
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($filepath)
$Worksheet = $Workbook.Worksheets.Item(1)
$startRow = 2

#create System.DataTable
    $dt = new-object "System.Data.DataTable"                                
    [void]$dt.Columns.Add("StaffStudentID", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("SurName", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("FirstName", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("Year", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("HomeGroup", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("DOB", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("StaffStudent", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("email", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("ImageName", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("CardKey", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("F11", [System.Type]::GetType("System.String"))
    Do {                    
        $ColValues1 = $Worksheet.Cells.Item($startRow, 2).Value()
        $ColValues2 = $Worksheet.Cells.Item($startRow, 3).Value()
        $ColValues3 = $Worksheet.Cells.Item($startRow, 4).Value()
        $ColValues4 = $Worksheet.Cells.Item($startRow, 5).Value()
        $ColValues5 = $Worksheet.Cells.Item($startRow, 6).Value()
        $ColValues6 = $Worksheet.Cells.Item($startRow, 7).Value()
        $ColValues7 = $Worksheet.Cells.Item($startRow, 8).Value()
        $ColValues8 = $Worksheet.Cells.Item($startRow, 9).Value()
        $ColValues9 = $Worksheet.Cells.Item($startRow, 10).Value()
        $ColValues10 = $Worksheet.Cells.Item($startRow, 11).Value()
        $ColValues11 = $Worksheet.Cells.Item($startRow, 12).Value()
        $startRow++
        $dt.Rows.Add($ColValues1,$ColValues2,$ColValues3,$ColValues4,$ColValues5,$ColValues6,$ColValues7,$ColValues8,$ColValues9,$ColValues10,$ColValues11)
        }
    While ($Worksheet.Cells.Item($startRow,2).Value() -ne $null)

$Excel.Quit()

#connect to SQL Server and import the system.data.table
$SQLServerConnection = "Data Source=$serverName;Integrated Security=true;Initial Catalog=$databaseName;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $SQLServerConnection
$bulkCopy.DestinationTableName = $tableName
$bulkCopy.WriteToServer($dt)


#Remove Veriables and com object 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Remove-Variable serverName
Remove-Variable databaseName
Remove-Variable tableName
Remove-Variable filepath
Remove-Variable excel
Remove-Variable workbook
Remove-Variable worksheet
Remove-Variable startRow
Remove-Variable dt
Remove-Variable bulkCopy
Remove-Variable sqlserverconnection

谢谢,

Wofen

1 个答案:

答案 0 :(得分:0)

您是否需要在退出()之前保存更改? 这种模式对我有用。

Powershell: Excel, save and close after run