Solved

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

Posted on 2013-02-01
5
1,324 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 142

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

810 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