Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-02-01
5
Medium Priority
?
1,494 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 35

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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.

636 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