Opening an Excel file in C# using Process.Start
Asked Answered
F

4

7

I'm trying to open an excel file using a button click. And for some reason it's not working. I've tried several things. Any ideas why they are not working?

Method 1 I have tried. This opens the file manager but does not open the proper file. It is definitely using the proper path to the file and the file does exist

private string fileCopy;

public RepairResultsControl()
{
    InitializeComponent();
}

public void Show(PSRepair.AnalysisResults analysis, string pathNameCopy)
{
    fileCopy = pathNameCopy;
    Show();
}

private void btnGoToFile_Click(object sender, EventArgs e)
{
    Process.Start("explorer.exe", "/select,"+ fileCopy);
}

Method 2. This just didn't open anything not sure why

System.Diagnostics.Process.Start(@"C:\Users\username\Documents\newTest.xlsx");
Footsie answered 1/12, 2016 at 14:49 Comment(9)
Are there any Excel processes lurking in the background?Ruvalcaba
My project is an excel addin therefore it is open in the background. Is this an issue?Footsie
Ah, don't know in that scenario. When I used to automate Excel, I'd end out with inactive processes that woudln't respond or open. But if this is an addin, don't you have access to the Excel object model to open the spreadsheet through that?Ruvalcaba
.. your first code example will open explorer with the file selected, as that's what explorer.exe /select does. However the second one should open the spreadsheet in Excel.Ruvalcaba
I'm not sure why it doesn't I can't think of what could be wrongFootsie
@Ruvalcaba the first one only opens file explorer it does not bring me to the file location. Any ideas why?Footsie
Seems to work for me here..Ruvalcaba
The value is the path file so C:\Users\username\Documents\newTest.xlsxFootsie
Maybe it's in use somewhere in my project or something but I assume it should still bring me to the file location?Footsie
A
11

Normally, Process.Start(@"C:\Users\username\Documents\newTest.xlsx"); would open your document in Excel.

However, you say in a comment that you are doing this from an Excel add-in which runs in the background. The solution needs to take this into account (the code sample assumes that you have a VSTO add-in, otherwise you need to adjust accordingly):

// make the running Excel instance visible
Globals.ThisAddIn.Application.Visible = true;

// open the workbook using Excel interop
Globals.ThisAddIn.Application.Workbooks.Open(fileName);
Akira answered 1/12, 2016 at 16:19 Comment(5)
I'm trying to open the xlsx file directly, and getting this exception: Win32Exception: The specified executable is not a valid application for this OS platform.Hint
This depends likely on the value of the UseShellExecute property. Make sure it is set to true.Akira
I was talking about Process.Start.Hint
Yes, pass in a ProcessStartInfo with that property set to true. If it still fails, you probably need to repair Office.Akira
"This depends likely on the value of the UseShellExecute property. Make sure it is set to true." was the culprit for me. Thanks a lot.Animalize
E
5

With .NET Core use:

Process.Start(new ProcessStartInfo{FileName = @"C:\Users\username\Documents\newTest.xlsx", UseShellExecute = true});
Epigeal answered 8/10, 2023 at 16:6 Comment(0)
D
0
  1. Try running as an admin
  2. Check for exceptions, also the start method should return a bool, check to make sure it is true.
  3. Make SURE your xlsx files are associated with Excel(easy check for this is at a command prompt, type in your filename and hit enter... if excel opens you are good)
  4. Check your system error logs.
Digress answered 1/12, 2016 at 16:6 Comment(0)
U
-1

In general, the following works:

public void OpenExcelFile(string filePath)
{
    System.Diagnostics.Process.Start(filePath);
}

If you could use interop, the following can be done:

public void OpenExcelFileAsRead(string filePath)
{
    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, ReadOnly: true);
    xlApp.Visible = true;
}
Undertrump answered 25/6 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.