I am working on ASP.NET application where there is a functionality for exporting realizations in .xls format. Every realization can have more projects, so each project should have separate sheet in excel. I have excel template that should be used for the making of excel tables. Problem is: this code create one empty template, and then it creates sheets for projects that are not using template. There are columns for days in time period that is chosen, weeks, action (things done in given time period), hours spent and details + description of work.I am using ClosedXML. I scrapped out unnecessary LINQs and cell merges.
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;
}
}