Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: Multiple SaveAs calls causes InvalidCastException [9483]

$
0
0
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.
Comments: 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.

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>