none
Issue when creating MemoryStream SpreadsheetDocument and downloading through browser RRS feed

  • Question

  • I have a very simple ASP.Net Core MVC app with a button that call a method in a class library in order to create a SpreadsheetDocument from randomly-generated integer values. When I run the app in Visual Studio 2017 with a single breakpoint that is hit once for each column of data I get the entire, valid, Excel file downloaded and it opens and displays perfectly. If I remove the breakpoint and run the app the file is created and downloaded but the columns are identical. I could really use some advice on this. Here is the MVC Controller method:

    public IActionResult Index()
    {
    	// Create an instance of the CreateExcel class.
    	var createExcel = new CreateExcel();
    
    	// Create a new Excel spreadsheet MemoryStream.
    	var excelSpreadsheet = createExcel.FetchNewMemorySpreadsheet();
    
    	// Return the spreadsheet.
    	return File(excelSpreadsheet, "application/vnd.ms-excel", "Report.xlsx");
    }

    Here is the method that oversees the creation of the SpreadsheetDocument:

    public Stream FetchNewMemorySpreadsheet()
    {
    	// Declare the object to return from the method.
    	SpreadsheetDocument spreadsheet = null;
    
    	//
    	try
    	{
    		// Make sure that the MemoryStream is valid.
    		if (_memStream == null) _memStream = new MemoryStream();
    
    		// Create a SpreadsheetDocument.
    		spreadsheet = CreateSpreadsheetWorkbook(_memStream);
    
    		// Create a Stylesheet.
    		CreateStylesheet(spreadsheet);
    
    		// Add static data to the spreadsheet.
    		FillSpreadsheet(spreadsheet);
    
    		// Close the document.
    		spreadsheet?.Close();
    
    		// Ensure that we have some data in the MemoryStream.
    		if (_memStream?.Length > 0)
    		{
    			// Seek to the beginning of the stream.
    			_memStream.Seek(0, SeekOrigin.Begin);
    		}
    
    		// Return the stream.
    		return _memStream;
    	}
    	catch (Exception e)
    	{
    		throw;
    	}
    	finally
    	{
    		// If the Spreadsheet is not null, call it's Dispose method.
    		spreadsheet?.Dispose();
    	}
    }

    Since the anomaly only occurs when I run without any breakpoints, I suspect that the problem might have something to do with the MemoryStream OR because I am not using some kind of asynchronous call.

    HELP! PLEASE!

    Thanks

    Sunday, June 4, 2017 7:47 PM

Answers

  • UPDATE: I changed the return type of the FetchNewMemorySpreadsheet method to be byte[] and modified the code in the MVC controller accordingly and all my issues went away. It was the MemoryStream causing the problem.
    • Marked as answer by JimNickFL Wednesday, June 7, 2017 7:36 AM
    Wednesday, June 7, 2017 7:36 AM

All replies

  • Hi JimNickFL,

    Could you share us a simple demo or complete code which could reproduce your issue?

    I created a new Asp.net Core MVC app, install package DocumentFormat.OpenXml, add your code to my project. But it missed objects like “_memStream,CreateSpreadsheetWorkbook,CreateStylesheet,FillSpreadsheet”.

    Based on your code, I did not find any specific code to cause this issue.

    I think a simple demo which could reproduce your issue would help us to narrow down this issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 5, 2017 6:05 AM
  • I'll post a short, complete, example in the next few hours. 

    Thanks

    Tuesday, June 6, 2017 7:21 AM
  • UPDATE: I changed the return type of the FetchNewMemorySpreadsheet method to be byte[] and modified the code in the MVC controller accordingly and all my issues went away. It was the MemoryStream causing the problem.
    • Marked as answer by JimNickFL Wednesday, June 7, 2017 7:36 AM
    Wednesday, June 7, 2017 7:36 AM