4

I've used the Open XML SDK 2.0 to create a new spreadhsheet and save to a users folder. i want to be able to open this automatically after saving. The file is in the xlsx format.

I tried

 SpreadsheetDocument.Open(fileName, true);

This isn't working at all. I'd like the code to open the file in whatever version of excel the user has - 2003, 2007 and 2010 (Assumption: 2003 will have the compatibility pack installed)

2 Answers 2

22

If excel is set as the default viewer for xls files on the system you can open the file using Process class:

System.Diagnostics.Process.Start("myFile.xls");
Sign up to request clarification or add additional context in comments.

Comments

0

This just opens the Excel for internal read/write in your app, but I assume you want to open it for the user in Excel?

Then you would have to do something like this:

using System.Diagnostics;

class Program
{
    static void Main()
    {
       // A.
       // Open specified Word file.
       OpenMicrosoftWord(@"C:\Users\Sam\Documents\Gears.docx");
    }

    /// <summary>
    /// Open specified word document.
    /// </summary>
    static void OpenMicrosoftWord(string f)
    {
       ProcessStartInfo startInfo = new ProcessStartInfo();
       startInfo.FileName = "WINWORD.EXE";
       startInfo.Arguments = f;
       Process.Start(startInfo);
    }
}

http://www.dotnetperls.com/process-start

3 Comments

I guess, have not tried that. But this should only block the UI until word is started.
yeah, UI blocked unti Word - Excel is started. Maybe is slower until the external program is opened. I have not found yet any good patterns about it in Windows Forms/Console app
Makes sense. You could try wrapping it into a Task

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.