Excel Process not closing in VB.net

后端 未结 8 1100
星月不相逢
星月不相逢 2020-12-03 09:00

I am creating an excel file using interop.excel and the process is not closing. This is the code i am trying to use.

 Private Sub converToExcel(fileLoc As S         


        
8条回答
  •  天命终不由人
    2020-12-03 09:17

    I did not see anyone properly address what was occuring and instead, tried to create work arounds for it.

    What is happening here is that the workbook is prompting, in the background, to be saved. In your code, you're saving the worksheet and not the workbook. You can either trick it and set the saved state of the workbook to true or save the workbook before exiting the excel applicaiton.

    I was also having this issue. The Excel process would run the entire time the application was open. By adding the xlWorkBook.Saved = True line, the process would end after the call to xlApp.Quit(). In my case, I did not need to save the excel file, only reference it for values.

    Option #1 - Do not save the workbook:

    xlWorkSheet.SaveAs(fileLoc)
    xlWorkBook.Saved = True       ' Add this line here.
    'xlWorkBook.Close()           ' This line shouldn't be necessary anymore.
    xlApp.Quit()
    

    Option #2 - Save the workbook to a new file:

    'xlWorkSheet.SaveAs(fileLoc)  ' Not needed
    xlWorkBook.SaveAs(fileLoc)    ' If the file doesn't exist
    'xlWorkBook.Close()           ' This line shouldn't be necessary anymore.
    xlApp.Quit()
    

    Option #3 - Save the workbook to an existing file:

    'xlWorkSheet.SaveAs(fileLoc)  ' Not needed
    xlWorkBook.Save(fileLoc)      ' If the file does exist
    'xlWorkBook.Close()           ' This line shouldn't be necessary anymore.
    xlApp.Quit()
    

    .Quit() Method:
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.quit?view=excel-pia#Microsoft_Office_Interop_Excel__Application_Quit

    .Saved() Method:
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._workbook.saved?view=excel-pia#Microsoft_Office_Interop_Excel__Workbook_Saved

提交回复
热议问题