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

Commented Issue: InvalidCastException on SaveAs [6719]

$
0
0
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 **

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;
```

![Image](http://img34.imageshack.us/img34/57/supt.png)

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


Viewing all articles
Browse latest Browse all 1877

Trending Articles



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