Bug in Excel.Save method?

Hey guys,

Getting a recurring exception thrown when I try to recover a Save that fails because the specified file is already open:



public bool Save(Excel excel, bool openContainingFolder, string suggestedFileName)

{

saveFileDialog.FileName = suggestedFileName;

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

try

{       
    <br>

excel.Save(saveFileDialog.FileName,
FileFormatType.ExcelXP);


}

catch (IOException)

{

    MessageBox.Show("Unable to save,

because the selected file is open and being used in another
window. Close it and try again.");

    return Save(excel,

openContainingFolder, suggestedFileName);

}

return true;

}

else

{

return false;

}

}



On the second save attempt (after the file that was open in another
Excel window was closed), the Excel.Save method throws the following
System.Exception: “The same worksheet name already exists”.



Bug?


This is a bug in old version. And do you use an evaluation copy?

Please try this attached fix.

Yes, for the moment we’re still using an evaluation copy. The retail copy is due in any day now.



I’ve updated to the new version dll and the exception has been replaced by:



An unhandled exception of type ‘System.ArgumentException’ occurred in Application.exe



Additional information: Item has already been added. Key in dictionary: “Workbook” Key being added: “Workbook”



Same code as before.

This exception doesn’t seem to be caused by Aspose.Excel. Please debug into your program to see what’s the exact reason. If you cannot find the clue, please post your project here. I will check it ASAP.

Stacktrace on the Exception:



at System.Collections.SortedList.Add(Object key, Object value)

at Aspose.Excel.Worksheets.֟(FileFormatType ֝)

at Aspose.Excel.Worksheets.֞(String ֡, SaveType ֢, FileFormatType ֝, HttpResponse ֣)

at Aspose.Excel.Excel.Save(String fileName, FileFormatType fileFormatType)

at Application.Dialogs.ExportDialog.Save(Excel excel, Boolean
openContainingFolder, String suggestedFileName)

at Application.Dialogs.ExportDialog.Save(Excel excel, Boolean
openContainingFolder, String suggestedFileName)

at Application.Dialogs.ExportDialog.buttonDownload_Click(Object
sender, EventArgs e)

at System.Windows.Forms.Control.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnClick(EventArgs e)

at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

at System.Windows.Forms.Control.WndProc(Message& m)

at System.Windows.Forms.ButtonBase.WndProc(Message& m)

at System.Windows.Forms.Button.WndProc(Message& m)

at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)

at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)

at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethod



Looks like there’s some strange code in Excel.Save that doesn’t clean
up properly, so that on the second attempt it’s trying to add an object
with a key that already exists.



Unfortunately, I can’t paste the current project, but I can try to
duplicate the error in a seperate project, if you like. Let me
know.

These strange code is caused by obfuscator.

A sample project for this problem will be helpful. Thank you very much.

Hi Laurence, Sorry about the long wait, we’ve been busy. I’ve
attached a Project that can reproduce the bug described above.

The steps to produce it are:

  1. Build & Run the project and save a test excel file with the dialog.
  2. Open the excel file in Excel, leaving it open.
  3. Run the project again and save the excel file as the same file
    again, to overwrite the file that is open in Excel. It will throw
    an IOException that will be caught by the code, which shows the dialog
    again, allowing the user to close the file before saving.
  4. Close the file in Excel.
  5. Save the file in the dialog (as the same file).



    Hope this helps. Any questions, please don’t hesitate to contact me:

    ICQ: 11203346

Thanks for your sample project. I fixed this problem. Please download and try v3.6.1.

Thanks Laurence. You guys are awesome!