2

Closed

Multiple SaveAs calls causes InvalidCastException

description

Version 0.76.0, Test case:
            XLWorkbook w = new XLWorkbook();
            IXLWorksheet ret = w.Worksheets.Add("test");
            w.SaveAs("test.xlsx");
            ret.Cell("A1").Value = "test";
            w.SaveAs("test"xlsx");
The first save succedes, the second one fails with error:

System.InvalidCastException: Unable to cast object of type 'DocumentFormat.OpenXml.Packaging.SharedStringTablePart' to type 'DocumentFormat.OpenXml.Packaging.WorksheetPart'.

Workaround:
            XLWorkbook w = new XLWorkbook();
            IXLWorksheet ret = w.Worksheets.Add("test");
            w.SaveAs(test.xlsx);
            w = new XLWorkbook(test.xlsx);
            w.Worksheets.Worksheet("test").Cell("A1").SetValue<string>("test");
            w.Save();
            w.Worksheets.Worksheet("test").Cell("A1").SetValue<string>("test2");
            w.Save();
I also tried Save() after first SaveAs() but it says that I cannot use "Save" for new files.
Closed Nov 11, 2016 at 10:51 AM by igitur

comments

masihemami wrote May 25, 2016 at 8:50 AM

Have you made any progress on this issue?

blackzork wrote May 25, 2016 at 9:15 AM

Yes. I just don't do it. It looks like it is a DocumentFormat.OpenXML issue. I changed my code to open and close XLWorkbook on every save.

masihemami wrote May 26, 2016 at 6:29 AM

Thanks for info. I did the same.

Pitterling wrote May 27, 2016 at 10:46 AM

blackzork,masihemami:

Please open an issue for OpenXML SDK:
OfficeDev/Open-XML-SDK

igitur wrote Nov 11, 2016 at 10:52 AM

This was indeed a bug in ClosedXML, not OpenXML.