?
Solved

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

Posted on 2013-02-01
5
Medium Priority
?
1,446 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
[X]
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
5 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 500 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 500 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 500 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 34

Accepted Solution

by:
Norie earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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