System.InvalidCastException was unhandled by user code
Message=Unable to cast object of type 'DocumentFormat.OpenXml.Packaging.SharedStringTablePart' to type 'DocumentFormat.OpenXml.Packaging.WorksheetPart'.
Source=ClosedXML
StackTrace:
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document)
at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath)
at ClosedXML.Excel.XLWorkbook.SaveAs(String file)
*
I haven't yet debugged ClosedXml, however, I am pretty sure I know where this comes from:
Before, we had new XLWorkbook(filename). Now we use a MemoryStream (created using the guideline at http://msdn.microsoft.com/en-us/library/ee945362%28v=office.11%29.aspx#InMemory_UsingMemoryStream). That's the only difference between a successful SaveAs call and a failing one.
I suspect it has to do with MemoryStream:
http://stackoverflow.com/questions/5842254/excel-and-unreadable-content-when-creating-an-open-xml-spreadsheet-with-memorys
or (ADDED):
http://amykinsgardiner.blogspot.com/2011/02/trouble-with-openxml-and-memorystreams.html
Comments: ** Comment from web user: stefek99 **
Message=Unable to cast object of type 'DocumentFormat.OpenXml.Packaging.SharedStringTablePart' to type 'DocumentFormat.OpenXml.Packaging.WorksheetPart'.
Source=ClosedXML
StackTrace:
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document)
at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath)
at ClosedXML.Excel.XLWorkbook.SaveAs(String file)
*
I haven't yet debugged ClosedXml, however, I am pretty sure I know where this comes from:
Before, we had new XLWorkbook(filename). Now we use a MemoryStream (created using the guideline at http://msdn.microsoft.com/en-us/library/ee945362%28v=office.11%29.aspx#InMemory_UsingMemoryStream). That's the only difference between a successful SaveAs call and a failing one.
I suspect it has to do with MemoryStream:
http://stackoverflow.com/questions/5842254/excel-and-unreadable-content-when-creating-an-open-xml-spreadsheet-with-memorys
or (ADDED):
http://amykinsgardiner.blogspot.com/2011/02/trouble-with-openxml-and-memorystreams.html
Comments: ** Comment from web user: stefek99 **
Using hello world and question on how to write to Memory Stream: https://closedxml.codeplex.com/discussions/248214
```
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
var ms = new MemoryStream();
workbook.SaveAs(ms);
return ms;
```

```
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document)
at ClosedXML.Excel.XLWorkbook.CreatePackage(Stream stream, Boolean newStream)
at ClosedXML.Excel.XLWorkbook.SaveAs(Stream stream)
```