Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hiiii

i used this code


C#
private void button3_Click(object sender, EventArgs e)
        {
            string path = @"C:\KBE\solid piston1.xls";

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

            oXL.Visible = false;

            oXL.DisplayAlerts = false;

            mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false);

            //Get all the sheets in the workbook

            mWorkSheets = mWorkBook.Worksheets;

            //Get the allready exists sheet

            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("INPUT");
            mWSheet1.Cells[3, 3] = cbf.Text;
            mWSheet1.Cells[8, 3] = comboBox2.Text;
            // mWSheet1.Cells[12, 3] = comboBox3.Text;
            mWSheet1.Cells[14, 3] = comboBox4.Text;
            mWSheet1.Cells[27, 3] = comboBox5.Text;

            mWSheet1.Cells[13, 3] = textBox46.Text;


            mWSheet1.Cells[4, 3] = textBox1.Text;
            mWSheet1.Cells[5, 3] = textBox2.Text;
            mWSheet1.Cells[6, 3] = textBox3.Text;
            mWSheet1.Cells[12, 3] = cbr.Text;

            mWSheet1.Cells[10, 3] = textBox6.Text;
            mWSheet1.Cells[11, 3] = textBox7.Text;
            mWSheet1.Cells[15, 3] = textBox8.Text;

            mWSheet1.Cells[26, 3] = textBox19.Text;
            mWSheet1.Cells[28, 3] = textBox20.Text;

            mWSheet1.Cells[29, 3] = textBox21.Text;
            mWSheet1.Cells[36, 3] = textBox22.Text;
            mWSheet1.Cells[31, 3] = textBox23.Text;
            mWSheet1.Cells[32, 3] = textBox24.Text;

            mWorkBook.Save();
            mWorkBook.Close();



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

            oXL.Visible = false;

            oXL.DisplayAlerts = false;

            mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false);

            //Get all the sheets in the workbook

            mWorkSheets = mWorkBook.Worksheets;
            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("INPUT");
            string j2 = (mWSheet1.Cells[12, 4].Text);
            label57.Text = j2.ToString();

            mWorkBook.Close();

            oXL.Quit();
            GC.Collect();

            Marshal.FinalReleaseComObject(mWSheet1);


            Marshal.FinalReleaseComObject(mWorkBook);


            Marshal.FinalReleaseComObject(oXL);





whenever i run this code and click button one or more times .so two or more excel.exe is open in task manager
so please give me suggestion what type of change is required in the given code






thanks
Posted

Dhaval,

I see this two times :

C#
oXL = new Microsoft.Office.Interop.Excel.Application();


However, I only see one group of

C#
oXL.Quit();
            GC.Collect();
            Marshal.FinalReleaseComObject(mWSheet1);
            Marshal.FinalReleaseComObject(mWorkBook);
            Marshal.FinalReleaseComObject(oXL);



Remember that before reasigning oXL, you must release it, otherwise the first reference is lost and the object remains.
 
Share this answer
 
Comments
Karthik_Mahalingam 9-Jan-14 9:21am    
5, good
you can also do one thing at the end as
oxl = null;
mwsheet1 = null;
Yes, your code is missing something. If you run your code multiple times, this is what you will get in task manager:

http://www.prairiefyre.com/KB/Uploads/Images/excelprocesses.jpg

You end up running several EXCEL.EXE in Task Manager.

So, this is what you can do:

Each process running in Windows will be assigned a Process ID. Capture the Process ID of the excel that you are running, then kill it when you have done your job.

Making the variable like this won't kill the process:
C#
oXL = null;

This is the sample codes:
C#
using System.Diagnostics;

Create a Dictionary at top level of your class:
C#
Dictionary<int,int> dicExcel = Dictionary(int,int>();
int MyExcelProcessId = 0;

Before you start creating an instance of Microsoft.Office.Interop.Excel object, you check for existing excel that are currently running:
C#
private void WriteExcel()
{
    // do something...

    CheckForExistingExcellProcesses();

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

    // do something...
}

void CheckForExistingExcellProcesses()
{
    Process[] AllProcesses = Process.GetProcessesByName("excel");

    foreach (Process ExcelProcess in AllProcesses)
    {
  	dicExcel.Add(ExcelProcess.Id, 1);
    }
}

After creating your instance of Microsoft.Office.Interop.Excel object, capture the process ID:
C#
private void WriteExcel()
{
    // do something...

    oXL = new Microsoft.Office.Interop.Excel.Application();
    GetTheExcelProcessIdThatUsedByThisInstance();

    // do something...
}

void GetTheExcelProcessIdThatUsedByThisInstance()
{
    Process[] AllProcesses = Process.GetProcessesByName("excel");

    // Search For the Right Excel
    foreach (Process ExcelProcess in AllProcesses)
    {
	if (dicExcel == null)
            return;

        if (dicExcel.ContainsKey(ExcelProcess.Id) == false)
        {
	    // This is the Process ID that used by your instance
	    MyExcelProcessId = ExcelProcess.Id;
            break;
	}
    }

    AllProcesses = null;
}

After you have done everything with your Excel, kill the process at the end:
C#
void KillExcelProcessThatUsedByThisInstance()
{
    Process[] AllProcesses = Process.GetProcessesByName("excel");

    foreach (Process ExcelProcess in AllProcesses)
    {
        if (ExcelProcess.Id == MyExcelProcessId)
        {
            ExcelProcess.Kill();
            break;
        }
    }
    AllProcesses = null;
}

The final code will be something like this:
C#
private void button3_Click(object sender, EventArgs e)
{
    CheckForExistingExcellProcesses();

    // do something...

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

    GetTheExcelProcessIdThatUsedByThisInstance();

    // execute your excel task ...blah..blah..blah... until end ..

    KillExcelProcessThatUsedByThisInstance();
}

I was once faced the same problem as you. Some programmers around the world have shared the knowledge to me, and now my turn to share this.

I have written a freeware based on this, you may want to have a look:
The Source Code of Simple Microsoft Excel Documents Converter[^]
 
Share this answer
 
v12
C#
mWorkBook.SaveAs(ref outputFileName, 
     ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
     ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing,
     ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing, ref _oMissing);

mWorkBook.Close(ref _oMissing, ref _oMissing, ref _oMissing);

oXL.Quit(ref _oMissing, ref _oMissing, ref _oMissing);
 
Share this answer
 
C#
Process [] proc Process.GetProcessesByName("ApplicationName");
    proc[0].Kill();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900