2007
05.28

[excerpt]On 18th May 2007, Tanmoy commented on “How To Kill MS Excel Process Started by ASP.NET”.

Initially on denial (^_^)/, I finally have time to investigate the issue. Tanmoy actually got it correctly. So I created a new GUI demo on how to correctly kill a particular Excel process.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel;
using System.Diagnostics;

namespace ThreadingGUI_01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ApplicationClass app = new ApplicationClass();
            if (app == null)
            {
                MessageBox.Show("Unable to Open Excel");
            }

            app.Visible = true;
            _Workbook wb = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            this.listBox1.Items.Add(app.Hwnd);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex >= 0)
            {
                Process[] procs = Process.GetProcessesByName("EXCEL");
                int selectedExcel = (Int32)this.listBox1.SelectedItem;
                foreach (Process p in procs)
                {
                    if (p.MainWindowHandle.ToInt32() == selectedExcel )
                    {
                        p.Kill();
                        this.listBox1.Items.Remove(selectedExcel);
                    }
                }
            }
        }
    }
}

Thanks to Tanmoy (^_^)/
[/excerpt]

Download the Project here.

Share the love:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Live
  • MySpace
  • StumbleUpon
  • Technorati
  • Reddit
  • HackerNews
  • Twitter

 

Advertise Here

 

1 comment so far

Add Your Comment
  1. [...] I have been working with ASP.NET Excel automation before, I still feel there are a lot of things that I don’t know. So as I browsed through the [...]