防止文件现在popupExcel

我在这里有一个问题,我一直在工作几个小时。

我正在导入一个Excel文件,我使用这个代码来做到这一点:

Dim objExcel As Excel.Application Dim objWorkBook As Excel.Workbook Dim totalWorkSheets As Excel.Worksheet Dim ExcelSheetName As String = "" objExcel = CreateObject("Excel.Application") objWorkBook = objExcel.Workbooks.Open(excelfile, Notify:=False) objExcel.DisplayAlerts = False Dim exConS As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & excelfile & ";Extended Properties=Excel 8.0;" For Each totalWorkSheets In objWorkBook.Worksheets ExcelSheetName += totalWorkSheets.Name Dim exCon As New OleDbConnection(exConS) Dim dsExcel As New DataSet() exCon.Open() Dim sExcel As String = "SELECT * FROM [" & totalWorkSheets.Name & "$]" Dim daExcel As New OleDbDataAdapter(sExcel, exCon) daExcel.Fill(dsExcel) exCon.Close() Next totalWorkSheets objWorkBook.Close() objExcel.Quit() Dim ggProcess As New Process ggProcess = Process.Start("EXCEL.EXE") ggProcess.Kill() 

问题是当我运行代码时,下面的对话框不断出现:

弹出

我怎样才能防止这个popup对话框出现?

正如poweruser所build议的,可以简单地改变

 objWorkBook = objExcel.Workbooks.Open(excelfile, Notify:=False) 

 objWorkBook = objExcel.Workbooks.Open(excelfile, Notify:=False, Readonly:=True) 

好吧,因为没有工作,我已经采取了更详细的代码,并认为你的问题是因为你打开Excel文件,然后创build一个外部连接到它。 该文件不必打开,以创build一个OLEDB连接。

看起来,你不提前知道标签名称,所以你需要打开文件来获取标签名称?

如果是的话,我会创build一个方法来打开文件获取标签名称,并返回一个标签名称的数组,然后你可以在上面的代码中遍历,然后closures文件。

 Use Set let TWB as workbook Set TWB=workbooks.open(Filename:="\\......", Readonly:=false, notify:=False) 

在Sube程序结束时将TWB设为无效。

从我的调查,如果你使用创build的Excel和快速连续退出,它不会立即closures以前退出Excel对象。 我注意到,在xlApp.Quit之后,即使我已经完成了所有的清理工作(closures工作表,将Excel COM对象设置为空),真正的EXCEL.exe过程消失了一段时间。

该解决scheme特别适用于我在testing中快速连续执行Excel导入/导出function的过程。

请注意,我的代码是从以下链接 derrived。

 Function Excel_Conversation() On Error GoTo Proc_Err Dim xlApp As Excel.Application, _ booLeaveOpen As Boolean 'if Excel is already open, use that instance booLeaveOpen = True 'attempting to use something that is not available 'will generate an error On Error Resume Next Set xlApp = GetObject(, "Excel.Application") On Error GoTo Proc_Err 'If xlApp is defined, then we 'already have a conversation If TypeName(xlApp) = "Nothing" Then booLeaveOpen = False 'Excel was not open -- create a new instance Set xlApp = CreateObject("Excel.Application") End If 'Do whatever you want Proc_Exit: On Error Resume Next If TypeName(xlApp) <> "Nothing" Then xlApp.ActiveWorkbook.Close False If Not booLeaveOpen Then xlApp.Quit Set xlApp = Nothing End If Exit Function Proc_Err: MsgBox Err.Description _ , , "ERROR " & Err.Number & " Excel_Conversation" 'comment next line after debugged Stop: Resume Resume Proc_Exit End Function