我在应用程序中使用 savefiledialog 来保存文件。如果相同的文件名已经存在,我会弹出一个询问我是否要替换的窗口。如果我不给,我会得到意外的系统异常。以下是部分代码
string fname1 = "";
saveFileDialog.Title = "Save the Proofer Report";
saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.InitialDirectory = "MyDocuments";
saveFileDialog.FileName = "Proofer Report";
aveFileDialog.AddExtension = true;
saveFileDialog.ShowHelp = true;
// saveFileDialog.ShowDialog();
Invoke((Action)(() => { saveFileDialog.ShowDialog(); }));
fname1 = saveFileDialog.FileName;
xlWorkBook.SaveAs(fname1, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
//system exception during save as
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
堆栈跟踪
在 Microsoft.Office.Interop.Excel._Workbook.SaveAs(对象文件名、对象 FileFormat、对象密码、对象 WriteResPassword、对象 ReadOnlyRecommended、对象 CreateBackup、XlSaveAsAccessMode AccessMode、对象 ConflictResolution、对象 AddToMru、对象 TextCodepage、对象 TextVisualLayout、对象本地)在ProoferXML.MainForm.ProcessDocument(BackgroundWorker worker, DoWorkEventArgs e) 在 D:\ProoferXML\WindowsFormsApplication1\WindowsFormsApplication1\MainForm.cs: 第 665 行在 ProoferXML.MainForm.prooferWorker_DoWork(Object sender, DoWorkEventArgs e) 在 D:\ProoferXML\WindowsFormsApplication1\WindowsFormsApplication1 \MainForm.cs:第 1457 行
使用 workbook.saveAs 保存 excel 时从 HRESULT 获取异常: