Search code examples
c#winformsexport-to-excel.net-6.0

DevExpress export GridView to Excel on .net6 'System.ComponentModel.Win32Exception'


I use this code to export gridview to excel on .Net Framework 4.7.2 without any problem

        using (XtraSaveFileDialog saveDialog = new XtraSaveFileDialog())
        {
            saveDialog.Filter = "Excel (2007-2019) (.xlsx)|*.xlsx";
            saveDialog.FileName = Resources.dailyProductionPaint;
            if (saveDialog.ShowDialog() != DialogResult.Cancel)
            {

                XlsxExportOptionsEx options = new XlsxExportOptionsEx();
                options.LayoutMode = DevExpress.Export.LayoutMode.Table;
                string exportFilePath = saveDialog.FileName;
                options.BeforeExportTable += ea =>
                {
                    ea.Table.Style.Name = XlBuiltInTableStyleId.None;
                };
                gridControl2.ExportToXlsx(exportFilePath, options);

                if (File.Exists(exportFilePath))
                {
                    try
                    {
                        //Try to open the file and let windows decide how to open it.
                        System.Diagnostics.Process.Start(exportFilePath);
                    }
                    catch
                    {
                        String msg = Resources.openFileEr + Environment.NewLine + Environment.NewLine + Resources.path + exportFilePath;
                        XtraMessageBox.Show(msg, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    String msg = Resources.saveFileEr + Environment.NewLine + Environment.NewLine + Resources.path + exportFilePath;
                    XtraMessageBox.Show(msg, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

but when I run the same code on .net6 I get this error

System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'C:\Users\TestUser\Desktop\Daily Result-Painting7.xlsx' with working directory 'C:\Users\TestUser\Desktop'. The specified executable is not a valid application for this OS platform.'

StackTrace

"at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)\r\n at System.Diagnostics.Process.Start()\r\n at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)\r\n at System.Diagnostics.Process.Start(String fileName)\r\n at SmartWinForm.PL.FrmAssemblyDailyProduction.btnExportExcel_ItemClick(Object sender, ItemClickEventArgs e) in C:\Users\TestUser\source\repos\SmartWinForm\SmartWinForm\PL\FrmAssemblyDailyProduction.cs:line 198\r\n at DevExpress.XtraBars.BarItem.OnClick(BarItemLink link)\r\n at DevExpress.XtraBars.BarBaseButtonItem.OnClick(BarItemLink link)\r\n at DevExpress.XtraBars.BarButtonItem.OnClick(BarItemLink link)\r\n at DevExpress.XtraBars.BarItemLink.OnLinkClick()\r\n at DevExpress.XtraBars.BarButtonItemLink.OnLinkClick()\r\n at DevExpress.XtraBars.BarButtonItemLink.OnLinkAction(BarLinkAction action, Object actionArgs)\r\n at DevExpress.XtraBars.ViewInfo.BarSelectionInfo.ClickLink(BarItemLink link)\r\n at DevExpress.XtraBars.ViewInfo.BarSelectionInfo.UnPressLink(BarItemLink link)\r\n at DevExpress.XtraBars.Controls.CustomLinksControl.OnMouseUp(MouseEventArgs e)\r\n at DevExpress.XtraBars.Controls.CustomPopupBarControl.OnMouseUp(MouseEventArgs e)\r\n at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at DevExpress.XtraBars.Controls.CustomControl.WndProc(Message& msg)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)"string


Solution

  • I get the answer from github thanks to kirsan31

    Process.Start(new ProcessStartInfo(exportFilePath) { UseShellExecute = true });
    

    In Core UseShellExecute property is set to false by default.