I am trying to export Excel Sheet from ASP.NET MVC 4 using Open XML SDK 2.5. I am trying to use standard examples from MSDN,
extention = "xlsx"; Response.ContentType = "application / vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader ("content-dispute", string.format ("attachment; file name = {0}. {1}", filename, extension)); (Using memorystream mem = new memorystream ()), create a spreadsheet document by supplying the {// file path. // By default, Autosave = true, editable =, and type = xlsx spreadsheet document spreadsheet document = spreadsheet document.create (mem, spreadsheet document type workbook); // Add the workbook to the document. Workbookpart Workbook part = Spreadsheet document. Workbookpart.Workbook = new workbook (); // Add the worksheet part to the workbook. Worksheetparty WorksheetPart = WorkbookPage. Addiction & Lieutenant; Worksheet part & gt; (); Worksheet Worksheet = new worksheet (new sheet data); // Add Sheet to Workbook. Sheet Sheet = Spreadsheet Document. Workbookper Workbook Appadyl & lt; Sheets & gt; (New sheets ()); // Attach a new worksheet and add it to the workbook Sheet Sheet = New Sheet () {ID = spreadsheet document. WorkbookParty.GateIDAffert (WorksheetPart), SheetId = 1, name = "MySet"}; Sheets.Append (sheet); Workbookpart.Workbook.Save (); // Close the document spreadsheetDocument.Close (); Mem.CopyTo (response.OutputStream); }
My file is being downloaded, but when I try to open it, a pop error says that the file is corrupted when I file with Notepad or Notepad ++ If the file is open then the file is completely empty and there is no XML data in it.
What am I missing and what should be done?
Thank you in advance.
Try to zero your memory stream position in front of you Copy it to copy: < / P>
// Close the document. SpreadsheetDocument.Close (); Mem.position = 0; Mem.CopyTo (response.OutputStream);
Alternatively, to copy the memory stream for the response, you can return FileStreamResult
from your MVC action:
< Code> public operation XlsDocument () {// Creating your document here ... // is very important to reset the memory stream in the initial state, otherwise you will get 0 bytes memoryStream.Position = 0; Var ResultsStream = New FileStream Ristol (memorystream, "app / vnd.openxmlformats-officedocument.spreadsheetml.sheet";); ResultStream.FileDownloadName = String.Format ("{0}. {1}", filename, extension); Return result serial; }
Comments
Post a Comment