objExcelBook = objExcelApp.Workbooks.Add()
objExcelSheet = objExcelApp.Worksheets.Add()
Dim filename As String = Left(Server.MapPath(Request.ApplicationPath), Len(Server.MapPath(Request.ApplicationPath)) - 7) & "\bc_web\downloadfile\gongsi" & Left(Trim(CStr(Now())), 10) & ".xls"
BindGrid() '绑定数据
' objExcelSheet.Columns("S:S").Select()
' objExcelApp.Selection.NumberFormatLocal = "@" ' objExcelSheet.Columns("AH:AH").Select()
'objExcelApp.Selection.NumberFormatLocal = "@" For j = 1 To cds.Tables("gskh").Columns.Count - 1
objExcelApp.Cells(1, j) = cds.Tables("gskh").Columns(j).ColumnName
Next j For i = 1 To cds.Tables("gskh").Rows.Count
For j = 1 To cds.Tables("gskh").Columns.Count - 1
objExcelApp.Cells(i + 1, j) = cds.Tables("gskh").Rows(i - 1).Item(j)
Next j
Next i
objExcelBook.SaveAs(filename)
objExcelBook.Close()
objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
objExcelApp = Nothing
objExcelBook = Nothing
objExcelSheet = Nothing
System.GC.Collect()加了 ' objExcelSheet.Columns("S:S").Select()
' objExcelApp.Selection.NumberFormatLocal = "@" ' objExcelSheet.Columns("AH:AH").Select()
'objExcelApp.Selection.NumberFormatLocal = "@"
后就QUIT不了EXCEL,运行后那EXCEL进程仍存在,用了
Public Sub KillExcel() '用来终止EXCEL进程
Dim pProcess() As System.Diagnostics.Process
pProcess = GetProcesses()
Dim i As Integer
For i = 0 To pProcess.Length() - 1
If (pProcess(i).ProcessName = "EXCEL.EXE") Then
pProcess(i).Kill() '关闭进程
Exit For
End If
Next
End Sub
又出现拒绝访问,救命啊
objExcelSheet = objExcelApp.Worksheets.Add()
Dim filename As String = Left(Server.MapPath(Request.ApplicationPath), Len(Server.MapPath(Request.ApplicationPath)) - 7) & "\bc_web\downloadfile\gongsi" & Left(Trim(CStr(Now())), 10) & ".xls"
BindGrid() '绑定数据
' objExcelSheet.Columns("S:S").Select()
' objExcelApp.Selection.NumberFormatLocal = "@" ' objExcelSheet.Columns("AH:AH").Select()
'objExcelApp.Selection.NumberFormatLocal = "@" For j = 1 To cds.Tables("gskh").Columns.Count - 1
objExcelApp.Cells(1, j) = cds.Tables("gskh").Columns(j).ColumnName
Next j For i = 1 To cds.Tables("gskh").Rows.Count
For j = 1 To cds.Tables("gskh").Columns.Count - 1
objExcelApp.Cells(i + 1, j) = cds.Tables("gskh").Rows(i - 1).Item(j)
Next j
Next i
objExcelBook.SaveAs(filename)
objExcelBook.Close()
objExcelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp)
objExcelApp = Nothing
objExcelBook = Nothing
objExcelSheet = Nothing
System.GC.Collect()加了 ' objExcelSheet.Columns("S:S").Select()
' objExcelApp.Selection.NumberFormatLocal = "@" ' objExcelSheet.Columns("AH:AH").Select()
'objExcelApp.Selection.NumberFormatLocal = "@"
后就QUIT不了EXCEL,运行后那EXCEL进程仍存在,用了
Public Sub KillExcel() '用来终止EXCEL进程
Dim pProcess() As System.Diagnostics.Process
pProcess = GetProcesses()
Dim i As Integer
For i = 0 To pProcess.Length() - 1
If (pProcess(i).ProcessName = "EXCEL.EXE") Then
pProcess(i).Kill() '关闭进程
Exit For
End If
Next
End Sub
又出现拒绝访问,救命啊
的方法是先得到系统进程列表,然后和“Excel”匹配,是Excel
的进程就杀死,我个人认为这个方法是不可行的,如果软件用这种方法
杀死自己启动的进程,应该算是Bug(有可能将用户Excel进程杀掉)。
我在网上找到了另一种杀死Excel 进程的方法,如下:
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
...
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
worksheets=null;
worksheet=null;
...
excelApp=null;
range=null;把操作Excel文件用到的对象实例全部释放。
然后资源回收!
GC.Collect();以上的代码最好能放在finally中,防止操作Excel文件时发生异常而执行不到!在打开任务管理器看看,excel进程是不是已经不在了!