是否可以在内存中创建电子表格并将其提供给 link 而无需将其保存在服务器端?
Is it possible to create a spreadsheet in memory and serve that to a link without saving it server-side?
我有 的标准代码,但我更愿意将其保存在内存中以供用户使用。 IE。当用户单击 link 我会生成它,用户可以将其保存在他们的 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();
}
}
我有
这是如何使用 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();
}
}