views:

1411

answers:

3

In a client-side web application, I would like to:

  1. open an Excel spreadsheet,
  2. export some application data to Excel,
  3. allow the user to work with it, and
  4. when they are done, read the (potentially changed) data back into my application.

I would like the user to have a fluid experience and detect when they are done with excel by hooking up to the BeforeClose event, but I find that I am unable to hook up to Excel's events in javascript/HTML.

function BeforeCloseEventHandler(cancel) {
    // TODO: read values from spreadsheet
    alert("Closing...");
}

function openExcel() {
    var excel = new ActiveXObject("Excel.Application");
    var workbook = excel.Workbooks.Add();
    var worksheet = workbook.Worksheets(1);
    worksheet.Cells(1, 1).Value = "First Cell";
    worksheet.Cells(1, 2).Value = "Second Cell";
    workbook.BeforeClose = BeforeCloseEventHandler;  // THIS DOESN'T WORK
    excel.Visible = true;
    excel.UserControl = true;
}

Does anyone have any suggestions?

A: 

I don't believe this is possible. The reason being, when you call the following code:

var excel = new ActiveXObject("Excel.Application");

You're actually opening up Excel. So with the following line:

workbook.BeforeClose = BeforeCloseEventHandler;

Its like you're telling the Excel application to run Javascript, which isn't possible. I've tried researching alternatives, like creating an event object, defining the code behind it, then assigning it to workbook.BeforeClose, but I would run into the same problem: Excel events can't be detected by javascript. Mainly because it runs as a seperate process.

So here's some more alternatives you may consider:

  1. Save the Excel data on the user's computer, then when the user loses excel, have them click somewhere that calls your 1st method, which reads that file and displays it.
  2. Read the data from the excel file and then display it.
  3. Don't close your excel object (this will probably leave excel open as a process on your computer), and have a timer event in javascript. Every 5 seconds, check if Excel is still open, and if it is not open, read the file and display it.

Sorry I couldn't be anymore help, and I'm not too sure if any of those alternatives would work, but good luck!

Anton
I don't have a clue about JavaScript, so I have no reason to doubt you here -- and hooking Excel via JavaScript sure does "sound" impossible! However, being out of process should not be a problem. Using standard out-of-process Automation using VB6, VB.NET or C# you absolutely CAN hook Excel events.
Mike Rosenblum
As an example, see "HOW TO: Handle Events for Excel by Using Visual C# .NET" at http://support.microsoft.com/kb/823981. But I still have NO clue how to do this via JavaScript, nor do I have any idea if it can be done...
Mike Rosenblum
+1  A: 

After doing some research, I have discovered that I cannot hook up events to dynamic ActiveX objects (i.e., the ones that are created by the new ActiveXObject constructor) in javascript.

One idea is that I create a wrapper Windows Form user control that would be hosted inside of an <object> tag in the web app. The user control would call Excel and receive events, and raise events back to javascript, which I could hook up to using the <script for="..." event="..."> mechanism. Not sure that this will work, but I will try it.

Even if it does work, I am not particularly happy about this solution. There are too many layers--the javascript is being called from a silverlight control meaning that my data has to cross 3 boundaries there and back: Silverlight -> Javascript -> Hosted Winform User Control -> Excel.

It would be nice to eliminate some of these boundaries.

Is this web app an intranet, used by corporate clients that can be guaranteed to have Excel installed? If not, then you will either get a crash in your code, or even if you handle the errors correctly, you'd have a big hole in the intended functionality of your web page.
Mike Rosenblum
Using a web-based grid control that you could embed I think would make more sense. If you don't need to allow the user to make formulas that reference other cells, grid controls are excellent for grid-style data input.
Mike Rosenblum
Yes, web app an intranet used by corporate clients guaranteed to have Excel installed.
I cannot use a grid--the requirement is to export data to Excel. This is what our customers want.
A: 

I think also your second approach using a Windows From control hosted in IE will not work.

IE behaves different as a scripting host. There are certain limitations as a blog post by Eric Lippert mentions:

Implementing Event Handling, Part Two

0xA3