|
-
February 27th, 2007, 12:25 AM
#1
Problem in running Excel-embeded .Net application
Following Microsoft KB instruction (http://support.microsoft.com/kb/304662), it is easy to embed an excel workbook (spreadsheet) in a .Net Winform program.
The real problem arises when there is a stand-along Excel application already running before launching an Excel-embedded Winform. The stand-along Excel application will hang up. It is believed that the Winform will take control of the external Excel instance. If more than one stand-along Excel instances are running, then the first one will be taken over by this Winform.
All Visual studio .Net versions have the same problem. Microsoft just totally ignored this obvious issue. Does anyone have a good idea on this problem? We have a not-so-elegant workaround for it. But I am looking for a better solution. Thank very much for your help.
-
February 27th, 2007, 03:37 PM
#2
Re: Problem in running Excel-embeded .Net application
I am not sure about IE but I have used another office launcher ocx control (released by MS) successfully in our project. If all else fails, you can give this control a try.
Visual C++ ActiveX Control for hosting Office documents in Visual Basic or HTML:
http://support.microsoft.com/kb/311765
Sample code
1. Register the ocx control in your machine.
2. Create a windows application
3. In the form, add dsoframer control (you should add the control first in your tool box
4. Reference Excel COM components (Microsoft.office.interop.excel).
5. add two buttons ("Open" and "Save") and attach the click even handler given below. Make sure to specify the correct file name.
Code:
private void btnOpen_Click(object sender, System.EventArgs e)
{
string inputFile = @"D:\temp\mergetest.xls";
OpenFile(inputFile);
}
/// <summary>
/// This method is used to open excel document in DSO framer control
/// </summary>
/// <param name="inputFile"></param>
private void OpenFile(string inputFile)
{
if (System.IO.File.Exists(inputFile))
{
object refmissing = System.Reflection.Missing.Value;
if (app.Workbooks.Count > 0)
app.Workbooks[1].Close(false, refmissing, refmissing);
Excel.Workbook newbook = app.Workbooks.Open(inputFile,
refmissing,refmissing,refmissing,refmissing,refmissing,
refmissing,refmissing,refmissing,refmissing,refmissing,
refmissing,refmissing, refmissing, refmissing);
// Do something with Workbook object
// Launch the workbook object in DSOFramer
axFramerControl1.Open(app.Workbooks[1], false, refmissing, "", "");
// When you again open the Workbook in DSOFramer, it seems it is taking
// a clone of the Workbook object. So, close the workbook object in Excel
// once it is opened in DSOFramer
if (app.Workbooks.Count > 0)
app.Workbooks[1].Close(true, refmissing, refmissing);
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (app != null)
{
object refmissing = Missing.Value;
// Close the workbook if it is still open in Excel.
if (app.Workbooks.Count > 0)
app.Workbooks[1].Close(true, refmissing, refmissing);
// Quit the excel application
app.Quit();
// release the excel COM object
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
string inputFile = @"D:\temp\mergetest.xls";
axFramerControl1.Save(inputFile, true, Missing.Value, Missing.Value);
}
When you deploy your applition in client's machine, this ocx control should also be registered in the client's machine.
-
February 28th, 2007, 09:41 PM
#3
Re: Problem in running Excel-embeded .Net application
Thanks for your reply. I noticed this method during my research. I am not sure if it will meet my need. I need to not only read in a file but also to programtically modify the worksheet. Anyway I will give it a try if the other method do not have a good solution.
-
March 1st, 2007, 10:55 PM
#4
Re: Problem in running Excel-embeded .Net application
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|