我正在开发ASP.NET应用程序,其中有一个以.xls格式导出实现的功能。每个实现都可以有更多的项目,因此每个项目都应该在excel中有单独的表。我有excel模板,应该用于制作excel表。问题是:此代码创建一个空模板,然后为不使用模板的项目创建工作表。选择的时间段内有列,周,行动(在给定时间段内完成的事情),花费的时间和细节+工作描述。我使用的是ClosedXML。我废弃了不必要的LINQ和单元格合并。
public FileContentResult Export(ExportRealizationViewModel newModel)
{
try
{
LINQ for employee whose realization is exported;
LINQ for projects in realizations;
XLWorkbook theWorkBook = new XLWorkbook(Server.MapPath(@"../Template.xlsx"));
foreach (var project in projects)
{
var wsheet = theWorkBook.Worksheets.Add(project);
}
using (theWorkBook)
{
foreach (IXLWorksheet ws in theWorkBook.Worksheets)
{
LINQ for date period of realizations;
LINQ for hours column in excel table;
LINQ for actions done during time period;
LINQ for action details;
LINQ for description of work;
int num = date.Count();
double summ = 0;
int count;
int cell;
int col;
double total;
StringBuilder sb = new StringBuilder();
// Cell merging for weeks column...
// Date column
... code for filling date column..
// Week column
... code for filling week column and cell formatting...
//Action column
... code for filling action column and cell formatting...
// Hours column (weekly and total)
... code for filling hours column and cell formatting...
//Remark column(details + description)
... code for filling remark column and cell formatting...
}
MemoryStream stream = new MemoryStream();
theWorkBook.SaveAs(stream);
byte[] fileBytes = stream.ToArray();
return File(fileBytes, "application/vnd.ms-excel", "NewFile.xls");
}
}
catch (Exception)
{
TempData["Error"] = "Error";
throw;
}
}