DevExpress 将 GridView 导出到 .net6 'System.ComponentModel.Win32Exception' 上的 Excel
DevExpress export GridView to Excel on .net6 'System.ComponentModel.Win32Exception'
我使用此代码将 gridview 导出到 .Net Framework 4.7.2 上的 excel,没有任何问题
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);
}
}
}
但是当我 运行 在 .net6 上使用相同的代码时,我得到了这个错误
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.'
堆栈跟踪
"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
感谢 kirsan31
,我从 github 得到了 the answer
Process.Start(new ProcessStartInfo(exportFilePath) { UseShellExecute = true });
在核心 UseShellExecute 中 属性 默认设置为 false。
我使用此代码将 gridview 导出到 .Net Framework 4.7.2 上的 excel,没有任何问题
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);
}
}
}
但是当我 运行 在 .net6 上使用相同的代码时,我得到了这个错误
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.'
堆栈跟踪
"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
感谢 kirsan31
,我从 github 得到了 the answerProcess.Start(new ProcessStartInfo(exportFilePath) { UseShellExecute = true });
在核心 UseShellExecute 中 属性 默认设置为 false。