Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Excel application does not quit after automation from Visual Studio .NET client

Excel application does not quit after automation from Visual Studio .NET client

Scheduled Pinned Locked Moved C#
csharpvisual-studiocomsysadmintesting
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xiaoming Qian
    wrote on last edited by
    #1

    Here is mycode: private void NullAndRelease(object runtimeObject) { try { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(runtimeObject); } catch {} finally { runtimeObject = null; } } private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); object NullParam = System.Reflection.Missing.Value; Workbook Book = ExcelApp.Workbooks.Open(FileName, 0, true, NullParam, NullParam, NullParam, true, NullParam, NullParam, false, false, NullParam, false, true, NullParam); Worksheet Sheet = Book.Worksheets[1] as Worksheet; Range UsedRange = Sheet.UsedRange; Range Cell = null; int RowCount = UsedRange.Rows.Count; for (int i = 2; i <= RowCount; i++) { Cell = UsedRange.Cells[i, 1] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 2] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 3] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 4] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 5] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 6] as Microsoft.Office.Interop.Excel.Range; NullAndRelease(Cell); } NullAndRelease(Cell); NullAndRelease(UsedRange); NullAndRelease(Sheet); if (Book != null) Book.Close(false, NullParam, false); NullAndRelease(Book); ExcelApp.Quit(); NullAndRelease(ExcelApp); GC.Collect(); GC.WaitForPendingFinalizers(); } These code works well.But when I add the following code in the for loop: Cell = UsedRange.Cells[i, 7] as Microsoft.Office.Interop.Excel.Range; The server does not shut down. It seems that I can only read not more than 6 cells.That's why?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups