openXML spreadsheetdocument return byte array for MVC file download

☆樱花仙子☆ 提交于 2019-11-30 14:53:07

问题


I'm trying to return a openXML spreadsheetdocument as a byte[] which I can then use to allow my MVC to send that file to a user. here is my spreadsheetdocument method to return the byte array

using (MemoryStream mem = new MemoryStream())
{
    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
        Create(mem, SpreadsheetDocumentType.Workbook);

    // Add a WorkbookPart to the document.
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    // Add Sheets to the Workbook.
    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
        AppendChild<Sheets>(new Sheets());

    SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

    //row start
    for (int id = 0; id <= reports.Count(); id++)
    {
        if (id == 0)
        {
            Row contentRow = CreateContentRow(reports.ElementAt(id), true, 2 + id);
            sheetData.AppendChild(contentRow);
        }
        else
        {
            Row contentRow = CreateContentRow(reports.ElementAt((id - 1)), false, 2 + id);
            sheetData.AppendChild(contentRow);
        }
    }

    // Append a new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet()
    {
        Id = spreadsheetDocument.WorkbookPart.
            GetIdOfPart(worksheetPart),
        SheetId = 1,
        Name = "mySheet"
    };
    sheets.Append(sheet);
    workbookpart.Workbook.Save();
    return mem.ToArray();
}

then in my MVC controller i call it like this

return File(crs.CreateReportSpreadSheet(reports),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Reports.xlsx");

the file downloads, but when you go to open it shows an error message that the file is corrupted. Is there any other way to allow them to download this file?


回答1:


I know that this is late, but I believe it is because you are not calling close on your spreadsheet document after you are finished with it. Make sure you close the document before returning the memory stream to commit all underlying streams used by the document.

// code omitted for clarity...
workbookpart.Workbook.Save();
spreadsheetDocument.Close();
return mem.ToArray()

http://msdn.microsoft.com/en-gb/library/documentformat.openxml.packaging.spreadsheetdocument(v=office.14).aspx




回答2:


I had a similar problem with a Word document. I solved this by using the byte array outsite the using, like:


byte[] returnBytes = null;
using (MemoryStream mem = new MemoryStream())
  {
    // your code
    returnBytes = mem.ToArray();
  }

return returnBytes;


来源:https://stackoverflow.com/questions/22101338/openxml-spreadsheetdocument-return-byte-array-for-mvc-file-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!