下面是我用ASP.NET开发的一个网站的代码片断,Private Declare Auto Function GetWindowThreadProcessId Lib "User32" (ByVal hWnd As Integer, ByRef intProcessID As Integer) As IntegerPrivate Sub SendExcel(ByVal dt As DataTable)
    Dim oExcelApp As Excel.Application = Nothing
    Dim oExcelBook As Excel.Workbook = Nothing
    Try
        操作EXCEL文件……
    Finally
        If Not oExcelApp Is Nothing Then
            Dim intProcessId As Integer = 0
            Dim intThreadId As Integer = 0
            Try
                If Not oExcelBook Is Nothing Then
                    oExcelBook.Close()
                    oExcelBook = Nothing
                End If
                oExcelApp.Workbooks.Close()
                oExcelApp.Quit()
                If oExcelApp.VBE.MainWindow.HWnd <> 0 Then
                    intThreadId = GetWindowThreadProcessId(oExcelApp.VBE.MainWindow.HWnd, intProcessId)
                End If
            Catch ex As Exception
            Finally
                oExcelApp = Nothing
                GC.Collect()
                GC.WaitForPendingFinalizers()
            End Try
            If intProcessId <> 0 Then
                System.Diagnostics.Process.GetProcessById(intProcessId).Kill()
            End If
        End If
    End Try
        发送EXCEL文件内容……
End Sub不知为什么GetWindowThreadProcessId函数返回的intProcessId和intThreadId总是0,我用C++和VB.NET编了两个小程序在同一时刻用相同的值试验都没问题。我猜可能是ASP.NET的用户和权限设置问题,也不知对不对,具体怎样设置就更不清楚了。没办法,只好请高手指教。顺便,当时因为EXCEL对象无法建立,我手工往web.config文件里加过<identity impersonate="true"/>这么一句。