|
Introduction
This article is about how to remove only run EXCEL.EXE without touching other open EXCEL.EXE file.
Problem
Some time we create Excel sheet report using c# programming. Hear I had a problem is clear EXCEL.EXE file from the task bar. I search lot on the internet but not find any solution for this. Finally I code my own and make it. Hear by I present my solution for you.
The Solution
I separate previous state of the process id an present state of the process Id both are filter by EXCEL.EXE format and put it in to separate arrays. From those arrays I find the newly open EXCEL.EXE file and kill the process.
The Implementation
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Microsoft.Office.Interop.Excel.Application xlApp;
private Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
private Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
private object missValues = System.Reflection.Missing.Value;
Process[] prs = Process.GetProcesses();
int[] array1 = new int[20];
int[] array2 = new int[20];
int[] array3 = new int[20];
private void button1_Click(object sender, EventArgs e)
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkbook = xlApp.Workbooks.Add(missValues);
xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
xlWorksheet.Cells[1, 1] = "Anuradha De Silva";
Microsoft.Office.Interop.Excel.Range HeaderRange1 = xlWorksheet.get_Range("A1", "A1");
HeaderRange1.Font.Bold = true;
HeaderRange1.Font.Size = 16;
xlWorksheet.Cells[2, 1] = "System Analyst";
Microsoft.Office.Interop.Excel.Range HeaderRange2 = xlWorksheet.get_Range("A2", "A2");
HeaderRange2.Font.Bold = true;
HeaderRange2.Font.Size = 14;
xlWorksheet.Cells[3, 1] = "Kanrich Finance LTD";
Microsoft.Office.Interop.Excel.Range HeaderRange3 = xlWorksheet.get_Range("A3", "A3");
HeaderRange3.Font.Bold = true;
HeaderRange3.Font.Size = 13;
xlWorksheet.Cells[4, 1] = "anuradhasanmo@gmail.com";
Microsoft.Office.Interop.Excel.Range HeaderRange4 = xlWorksheet.get_Range("A3", "A3");
HeaderRange4.Font.Bold = true;
HeaderRange4.Font.Size = 13;
xlApp.Visible = true;
Marshal.ReleaseComObject(xlWorksheet);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);
int i = 0;
foreach (Process p in prs)
{
if (p.ProcessName == "EXCEL")
{
array1[i] = p.Id;
i++;
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Process[] prs1 = Process.GetProcesses();
int i = 0;
foreach (Process p in prs1)
{
if (p.ProcessName == "EXCEL")
{
array2[i] = p.Id;
i++;
}
}
int h = 0;
for (int x = 0; x < array2.Length; x++)
{
for (int g = 0; g < array1.Length; g++)
{
if (array2[x] != 0)
{
if (array1[g] == array2[x])
{
h++;
}
else
{
if (array1.Length - 1 == g & h != 1)
{
array3[x] = array2[x];
}
}
}
}
h = 0;
}
foreach (Process p in prs1)
{
if (p.ProcessName == "EXCEL")
{
for (int x = 0; x < array1.Length; x++)
{
if (array3[x] != 0)
{
if (array3[x] == p.Id)
{
p.Kill();
return;
}
}
}
}
}
}
}
}
About The Author
Anuradha De Silva (Senior Software Engineer)
Languages: C#, VB.6.
Tools: Visual Studio 2010.
Databases: MS SQL Server, Oracle, SQLite.
Skills: Bsc(Hons)IT
Likes: Solving problems at projecteuler.net. at #64 now.
I have been a programmer since 2004.My first experience with BASIC the program worked but it contained an error. After I move to VB6 and provide many industrial solutions between 2004 to 2007.My turning point with the starting C#. I worked many company as a software engineer, senior software engineer and project manager.Curently I work as a system analyst in finance industry and provide many solution in .NET base.
|
|
|
|