Click here to Skip to main content
15,916,280 members
Home / Discussions / C#
   

C#

 
AnswerRe: Problem when convert 50MB string values to Stream... Pin
Luc Pattyn7-Aug-09 3:42
sitebuilderLuc Pattyn7-Aug-09 3:42 
QuestionOpen new tab that not exist Pin
tamir9017-Aug-09 1:43
tamir9017-Aug-09 1:43 
AnswerRe: Open new tab that not exist Pin
Arindam Sinha7-Aug-09 1:58
Arindam Sinha7-Aug-09 1:58 
GeneralRe: Open new tab that not exist Pin
tamir9017-Aug-09 2:04
tamir9017-Aug-09 2:04 
GeneralRe: Open new tab that not exist Pin
Arindam Sinha7-Aug-09 3:20
Arindam Sinha7-Aug-09 3:20 
AnswerRe: Open new tab that not exist Pin
Moreno Airoldi7-Aug-09 2:13
Moreno Airoldi7-Aug-09 2:13 
GeneralRe: Open new tab that not exist Pin
Luc Pattyn7-Aug-09 3:45
sitebuilderLuc Pattyn7-Aug-09 3:45 
GeneralRe: Open new tab that not exist Pin
Moreno Airoldi7-Aug-09 15:18
Moreno Airoldi7-Aug-09 15:18 
GeneralRe: Open new tab that not exist Pin
Luc Pattyn7-Aug-09 15:40
sitebuilderLuc Pattyn7-Aug-09 15:40 
GeneralRe: Open new tab that not exist Pin
tamir90112-Aug-09 5:08
tamir90112-Aug-09 5:08 
GeneralRe: Open new tab that not exist Pin
Moreno Airoldi12-Aug-09 6:56
Moreno Airoldi12-Aug-09 6:56 
GeneralRe: Open new tab that not exist Pin
tamir90112-Aug-09 18:54
tamir90112-Aug-09 18:54 
GeneralRe: Open new tab that not exist Pin
Moreno Airoldi13-Aug-09 0:31
Moreno Airoldi13-Aug-09 0:31 
QuestionProblem to Export DataTable to Excel. Pin
hdv2127-Aug-09 1:10
hdv2127-Aug-09 1:10 
AnswerRe: Problem to Export DataTable to Excel. Pin
Tamer Oz7-Aug-09 1:37
Tamer Oz7-Aug-09 1:37 
GeneralRe: Problem to Export DataTable to Excel. Pin
hdv2127-Aug-09 2:28
hdv2127-Aug-09 2:28 
Questionwhen is Excel shutdown completed? Pin
Harry6667-Aug-09 1:07
Harry6667-Aug-09 1:07 
AnswerRe: when is Excel shutdown completed? Pin
Moreno Airoldi7-Aug-09 2:03
Moreno Airoldi7-Aug-09 2:03 
QuestionRe: when is Excel shutdown completed? Pin
Harry6667-Aug-09 5:06
Harry6667-Aug-09 5:06 
AnswerRe: when is Excel shutdown completed? Pin
Moreno Airoldi7-Aug-09 15:12
Moreno Airoldi7-Aug-09 15:12 
GeneralRe: when is Excel shutdown completed? Pin
Harry66610-Aug-09 23:29
Harry66610-Aug-09 23:29 
GeneralRe: when is Excel shutdown completed? Pin
Moreno Airoldi11-Aug-09 0:10
Moreno Airoldi11-Aug-09 0:10 
GeneralRe: when is Excel shutdown completed? Pin
Harry66611-Aug-09 3:20
Harry66611-Aug-09 3:20 
GeneralRe: when is Excel shutdown completed? Pin
Moreno Airoldi12-Aug-09 1:24
Moreno Airoldi12-Aug-09 1:24 
Yes you are right! Calling Process.Start() before Application.Quit() won't work properly. I made some tests and I confirm it.

The problem is in how Excel optimizes opening documents. Consider the following example (derived from my previous):

C#
Microsoft.Office.Interop.Excel.Application myApp = null;
Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
try
{
    try
    {
        myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        myApp.Visible = true;
        myApp.AskToUpdateLinks = false;
        myApp.DisplayAlerts = false;
        myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        myWorkbook.SaveAs("D:\\test.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
            System.Type.Missing, System.Type.Missing, false, false,
            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

    }
    finally
    {
        if (myWorkbook != null)
        {
            myWorkbook.Close(false, "", null);
            myWorkbook = null;
        }
    }

    System.Threading.Thread.Sleep(3000);
    System.Diagnostics.Process.Start("D:\\test.xls");
    System.Threading.Thread.Sleep(3000);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
    if (myApp != null)
    {
        myApp.Quit();
        myApp = null;
    }
}


You will clearly see that since one Excel task is still active (the one we instanced for opening and saving the workbook), the call to Process.Start() will cause it to be the container for the re-opening of the document. Since we call Application.Quit() right after that, we close the workbook! This "task optimization" is an Excel internal, so I doubt there's any way to bypass it.

All the other differences between your code and my example don't seem to count: it's all basically the same.

I think you'll just have to call Application.Quit() before opening the file. There's probably no way to avoid that (unless you use another application - not Excel - to show the file...).

If you are working on multiple documents, and you want to open each saved file while processing the next, you may consider opening a different Excel.Application for each single one. There will sure be an overhead, but you can make some tests and see if it's acceptable (it probably is).

This (brutal) example works fine:

C#
for (int i = 1; i < 6; i++)
{
    Microsoft.Office.Interop.Excel.Application myApp = null;
    Microsoft.Office.Interop.Excel.Workbook myWorkbook = null;
    try
    {
        try
        {
            myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myApp.Visible = true;
            myApp.AskToUpdateLinks = false;
            myApp.DisplayAlerts = false;
            myWorkbook = myApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            myWorkbook.SaveAs("D:\\test" + i.ToString() + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet,
                System.Type.Missing, System.Type.Missing, false, false,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Type.Missing,
                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

        }
        finally
        {
            if (myWorkbook != null)
            {
                myWorkbook.Close(false, "", null);
                myWorkbook = null;
            }
            if (myApp != null)
            {
                myApp.Quit();
                myApp = null;
            }
        }

        System.Diagnostics.Process.Start("D:\\test" + i.ToString() + ".xls");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}


Good luck, let me know how it goes! Smile | :)

2+2=5 for very large amounts of 2
(always loved that one hehe!)

GeneralRe: when is Excel shutdown completed? Pin
Harry66612-Aug-09 2:09
Harry66612-Aug-09 2:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.