c# - 是否可以在内存中创建电子表格并将其提供给链接而不将其保存在服务器端?
问题描述
我有使用 OpenXml 创建电子表格的标准代码,但我更愿意将其保存在内存中为用户服务。即,当用户单击链接时,我会生成它,并且用户可以将其保存在他们的 PC 上。
这是如何使用 OpenXml 和 MVC 完成的?保存每次都需要重新生成的文件是否有固有的优势?
解决方案
所以一切都在一个地方:
在视图中:
<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();
}
}
推荐阅读
- reactjs - Openlayers 修改交互
- c++ - 如何通过时间跟踪屏幕?
- java - 休眠延迟加载不适用于 java 11 中的 forEach()
- c# - 必须至少有一个字母的正则表达式
- python - XML file processing subtags or make the number of subtags consistent
- firebase - Vuex and Firebase, store additional user info
- apache-kafka - 如何使用“MQTT 协议与 Kafka 作为代理”?
- mysql - 为什么mysql workbench 8.0会因为不关闭if而抛出错误,尽管所有“if”都已正确关闭?
- javascript - 是否可以在 html 文件中动态加载来自 js 文件的图像?
- prolog - labeling/2 如何从域的中点生成解决方案?