OpenXML的小号preadsheetdocument返回字节数组MVC文件下载 [英] openXML spreadsheetdocument return byte array for MVC file download

查看:153
本文介绍了OpenXML的小号preadsheetdocument返回字节数组MVC文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回OpenXML的小号preadsheetdocument为一个byte [],我可以再使用,使我的MVC将该文件发送给用户。这里是我的小号preadsheetdocument方法返回的字节数组

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();
        }

然后在我的MVC控制器我这样称呼它

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?

推荐答案

我知道这是晚了,但我相信这是因为你你用它完成后,不会调用close对您的S preadsheet文件。请确保您返回内存流提交文档使用所有底层流之前关闭该文档。

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()


<一href=\"http://msdn.microsoft.com/en-gb/library/documentformat.openxml.packaging.s$p$padsheetdocument(v=office.14).aspx\">http://msdn.microsoft.com/en-gb/library/documentformat.openxml.packaging.s$p$padsheetdocument(v=office.14).aspx

这篇关于OpenXML的小号preadsheetdocument返回字节数组MVC文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆