0

After my program creates an Excel file, I let the user see that file.

Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook excelWorkbook = xlApp.Workbooks.Open(xlsx.saveLocation + "\\" + xlsx.fileName,
    0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
    true, false, 0, true, false, false);

Whenever I close the Excel window, the process (EXCEL.EXE) remains (I'm hitting the red X in the top right). Am I missing a setting or something? I want the Excel window to be independent of the C# program, so that if I end the latter the former will still be visible.

2 Answers 2

2

If you want true independence, then you should use Process.Start to start Excel.

The simplest version you'd need would be:

Process.Start(Path.Combine(xlsx.saveLocation, xlsx.fileName));

though I'd recommend using the proper Path methods for combining the directory and filename into a complete path. Path.Combine

Source

If your path or filename has spaces then you need to wrap the whole thing in quotes:

var arguments = new StringBuilder("\"");
arguments.Append(Path.Combine(xlsx.saveLocation, xlsx.fileName));
arguments.Append("\"");
Process.Start(arguments.ToString());

(Though a StringBuilder may be overkill in this scenario)

Sign up to request clarification or add additional context in comments.

2 Comments

xlsx.fileName has a space in it ("Journal 05032"). How can I use that as an argument in Process.Start()? First it tries to open Journal.xlsx, and then it tries 05032.xlsx
@Andrew - Wrap the whole thing in quotes.
2

You should open the file by calling Process.Start(filename).
This will open the file in the user's default program, without tying your code to Excel.

If you need to manipulate Excel, that won't help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.