Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# closing an instance of  Microsoft.Office.Interop.Excel.Application excelApp

Posted on 2013-02-01
5
1,334 Views
Last Modified: 2013-04-02
Have tried a few including excelApp.ActiveWorkbook.Close(0,0,0); but it does not always close down the version thats open any ideas please
It seems to add 4 then take of 3 so that one stays and the open instances of excel seems to grow

// FIND ALL THE DATA OFF THE SPEAD SHEET FOR THE DISECTION INTO THE ARRAY
        private void GetCSVData(String filePath)
        {
          //  String filePath = @"C:\aeltest\@3rdmill.csv";
            // EXTRACT THE FILE NAME FOR THE SITE
            int startstr = filePath.IndexOf("@");
            string FileNme = filePath.Substring(startstr, filePath.Length - startstr);

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

            for (int intI = 1; intI < 100; intI++)
            {
                Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 1];
                Microsoft.Office.Interop.Excel.Range range2 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 2];
                Microsoft.Office.Interop.Excel.Range range3 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 3];
                Microsoft.Office.Interop.Excel.Range range4 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 4];
                Microsoft.Office.Interop.Excel.Range range5 = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[intI, 5];

                try
                {
                    string cellValue1 = range1.Value2.ToString();
                    string cellValue2 = range2.Value2.ToString();
                    string cellValue3 = range3.Value2.ToString();
                    string cellValue4 = range4.Value2.ToString();
                    string cellValue5 = range5.Value2.ToString();

                    String abc = cellValue1;
                    CSVData[intI, 1] = cellValue1;
           //         PullApartTheCSVString( cellValue1, cellValue2, cellValue3, cellValue4, cellValue5, intI);
                    savetoDB( FileNme, cellValue1, cellValue2, cellValue3, cellValue4, cellValue5, intI);

                    //        savetoDB( String FileName, int intI);

                }
                catch
                {
                  //  intI = 101;
                }
            }
*********************************************************
             excelApp.ActiveWorkbook.Close(0,0,0);
        }
0
Comment
Question by:sydneyguy
5 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 125 total points
ID: 38846182
Try writing this in the finally block
finally
{
excelApp.ActiveWorkbook.Close(0,0,0);
}

Open in new window



Or by using a using keyword for this

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 38846251
you have indeed to close and even dispose all objects (using using suggested above is the best method for that.
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 125 total points
ID: 38846489
Dim worksheet As New Microsoft.Office.Interop.Excel.Worksheet
worksheet = CType(excel.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
CType(worksheet, Microsoft.Office.Interop.Excel._Worksheet).Activate()
    '        ' If wanting excel to shutdown...
CType(excel, Microsoft.Office.Interop.Excel._Application).Quit()
0
 
LVL 33

Accepted Solution

by:
Norie earned 125 total points
ID: 38846594
If you want to quit the Excel application wouldn't it be this:
excelApp.Quit();

Open in new window


This looks more like it's for closing a workbook.
excelApp.ActiveWorkbook.Close(0,0,0);

Open in new window

0
 

Author Closing Comment

by:sydneyguy
ID: 39042665
thanks for all your help got it figured out in the end thanks to all your help
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question