-1

When trying to export data from DataGridView to excel System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.' exception is thrown, funny thing is that file is saved like supposed to. I fought that this is because I reinstalled packages for entire solution, but it's not the case.

Exception occurs when trying to call Process.Start(fileName) method, where the value of fileName is: C:\\Users\\net\\Desktop\\Excel TESTING\\OperatorStatisticsData.xlsx

Note that my application is running on any CPU( 32 or 64 bit), and I am currently running win10 64 bit operating system. enter image description here Any suggestion how to fix this?

Djordje
  • 437
  • 1
  • 12
  • 24
  • 1
    @mjwills my apologies, there are too many methods, i fought the information about reinstalling packages would be enough, check out my [edited](https://stackoverflow.com/q/48005685/7517846) question – Djordje Dec 28 '17 at 10:32
  • @mjwills `"C:\\Users\\net\\Desktop\\Excel TESTING\\OperatorStatisticsData.xlsx"` no need for downvote – Djordje Dec 28 '17 at 10:35
  • @mjwills tried it, still same, but funny thing as i mentioned is that data is saved like supposed to – Djordje Dec 28 '17 at 10:41

1 Answers1

3

Try to call EXCEL.EXE directly (change path if need to):

string filePath = @"c:\Temp\export.XLSX";
// For me this generates: C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE
var exec = System.IO.Path.Combine(
                Environment.GetEnvironmentVariable("ProgramW6432"),
                "Microsoft Office", "Office14", "EXCEL.EXE");
Process.Start(fileName: exec, arguments: filePath);

UPDATE

You can obtain executable file by using Shell AssocQueryString function, which retrieves (amongst other properties) executable file associated with extension. Here's the method on C# which makes use of it:

[Flags]
public enum AssocF
{
    Init_NoRemapCLSID = 0x1,
    Init_ByExeName = 0x2,
    Open_ByExeName = 0x2,
    Init_DefaultToStar = 0x4,
    Init_DefaultToFolder = 0x8,
    NoUserSettings = 0x10,
    NoTruncate = 0x20,
    Verify = 0x40,
    RemapRunDll = 0x80,
    NoFixUps = 0x100,
    IgnoreBaseClass = 0x200
}

public enum AssocStr
{
    Command = 1,
    Executable,
    FriendlyDocName,
    FriendlyAppName,
    NoOpen,
    ShellNewValue,
    DDECommand,
    DDEIfExec,
    DDEApplication,
    DDETopic
}

public static class FileAssocHelper
{

    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);

    public static string FileExtensionInfo(AssocStr assocStr, string doctype)
    {
        uint pcchOut = 0;
        AssocQueryString(AssocF.Verify, assocStr, doctype, null, null, ref pcchOut);
        StringBuilder pszOut = new StringBuilder((int)pcchOut);
        AssocQueryString(AssocF.Verify, assocStr, doctype, null, pszOut, ref pcchOut);
        return pszOut.ToString();
    }

}

Now we can retrieve executable file and open Excel file:

string filePath = @"c:\Temp\Results.xlsx";
string exec = FileAssocHelper.FileExtensionInfo(AssocStr.Executable, ".xlsx");
Process.Start(exec, filePath);
JohnyL
  • 6,894
  • 3
  • 22
  • 41
  • I salute you :D, took me a while to notice that my "Excel.EXE" is in "PrgoramFiles(x86)" so I've changed path `Environment.GetEnvironmentVariable("ProgramFiles(x86)"), "Microsoft Office", "Office14", "EXCEL.EXE");` – Djordje Dec 28 '17 at 14:32
  • is there a way I could dynamically get "EXCEL.EXE" path? – Djordje Dec 28 '17 at 14:38
  • 1
    @Yollo Glad it worked! I thought about dynamically finding EXCEL.EXE too, but for now can't tell something concrete. If I find a solution, I'll update an answer. :) – JohnyL Dec 28 '17 at 15:17
  • 1
    @Yollo I have found solution and will update an answer :) – JohnyL Dec 28 '17 at 20:12
  • thank you, stackoverflow literally schooled me :D , until your answer I didn't know why and where exactly this error occurs, only thing now is to make application portable xd. – Djordje Dec 28 '17 at 22:22