首页 > 解决方案 > 是否可以在内存中创建电子表格并将其提供给链接而不将其保存在服务器端?

问题描述

我有使用 OpenXml 创建电子表格的标准代码,但我更愿意将其保存在内存中为用户服务。即,当用户单击链接时,我会生成它,并且用户可以将其保存在他们的 PC 上。

这是如何使用 OpenXml 和 MVC 完成的?保存每次都需要重新生成的文件是否有固有的优势?

标签: c#asp.net-mvcopenxml

解决方案


所以一切都在一个地方:

在视图中:

<a href="@Url.Action("GetRejects", "Home")" style="color:#ac0066"><span class="glyphicon glyphicon-download"></span> Download list of rejected invoices</a>

在控制器中:

public FileContentResult GetRejects()
        {
            return File(WorksheetTools.GetRejectsExcelFile(userId),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                string.Concat("Rejects.", DateTime.Now.ToString("ddMMyyyy.HHmmss"), ".xlsx"));
        }

GetRejectsExcelFile 函数:

public static byte[] GetRejectsExcelFile(string userId)
        {
            List<RejectsModel> rejectList = GetFactureFournisseur(userId);

        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>();
            SheetData sheetData = new SheetData();
            worksheetPart.Worksheet = new Worksheet(sheetData);

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

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "Invoices"
            };

            Row row = new Row() { RowIndex = 1 };
            Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Business"), DataType = CellValues.String };
            row.Append(header1);
            Cell header2 = new Cell() { CellReference = "B1", CellValue = new CellValue("Supplier code"), DataType = CellValues.String };
            row.Append(header2);
            Cell header3 = new Cell() { CellReference = "C1", CellValue = new CellValue("Supplier"), DataType = CellValues.String };
            row.Append(header3);

            sheetData.Append(row);

            uint rowIndex = 1;
            foreach(var ff in rejectList)
            {
                ++rowIndex;
                Row dataRow = new Row() { RowIndex = rowIndex };
                dataRow.Append(new Cell() { CellReference = "A" + rowIndex.ToString(), CellValue = new CellValue(ff.BusinessName), DataType = CellValues.String });
                dataRow.Append(new Cell() { CellReference = "B" + rowIndex.ToString(), CellValue = new CellValue(ff.SupplierCode), DataType = CellValues.String });
                dataRow.Append(new Cell() { CellReference = "C" + rowIndex.ToString(), CellValue = new CellValue(ff.SupplierName), DataType = CellValues.String });

                sheetData.Append(dataRow);
            }

            sheets.Append(sheet);
            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();

            return mem.ToArray();
        }
    }

推荐阅读