Nuget Package install
PM> Install-Package GemBox.Spreadsheet
private void GenerateExcel(ResultVM resultViewModel,ProjectOutput projectOutput, OutputIndicator outputIndicator, IEnumerable<IndicatorMilestone> indicatorMilestone)
{
if (projectOutput != null)
{
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
ExcelFile ef = ExcelFile.Load(Server.MapPath("~/SpreadsheetTemplate/Logframe_Template.xlsx"));
ExcelWorksheet ws = ef.Worksheets[0];//Test
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Rows.Add(new object[] { 106, "Mario", "Rossi" });
//ws.Columns.InsertEmpty(15, dt.Columns.Count);
ws.InsertDataTable(dt, new InsertDataTableOptions()
{
ColumnHeaders = true,
StartRow = 0,
StartColumn = 15
});
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats";
Response.AddHeader("Content-Disposition", "attachment; filename=" + "Logframe-" + projectOutput.ProjectID + ".xlsx");
MemoryStream ms = new MemoryStream();
ef.SaveXlsx(ms);
ms.WriteTo(Response.OutputStream);
Response.End();
//test ends
//----Fillup Output------//
ws.Cells["B2"].Value = resultViewModel.ProjectHeader.Title; //Project Title
ws.Cells["A45"].Value = projectOutput.ProjectOutputDescription;
ws.Cells["H45"].Value = projectOutput.Assumption;
ws.Cells["A48"].Value = projectOutput.ImpactWeightingPercentage + "%";
ws.Cells["H48"].Value = RiskText(projectOutput.Risk);
//----Fillup Indicator------//
ws.Cells["B45"].Value = outputIndicator.SDPIndicator ?? outputIndicator.IndicatorTitle;
ws.Cells["C48"].Value = outputIndicator.Source;
ws.Cells["D45"].Value = outputIndicator.Baseline;
//----Fillup Milestone planned and Achieved------//
ws.Cells["E45"].Value = indicatorMilestone.Where(x => x.MilestoneID == ((int)indicatorMilestone.Min(r => r.MilestoneID))).Select(x => x.Planned).First();
ws.Cells["F45"].Value = indicatorMilestone.Where(x => x.MilestoneID == ((int)indicatorMilestone.Min(r => r.MilestoneID))+1).Select(x => x.Planned).First();
ws.Cells["G45"].Value = indicatorMilestone.Where(x => x.MilestoneID == ((int)indicatorMilestone.Min(r => r.MilestoneID))+2).Select(x => x.Planned).First();
ws.Cells["E46"].Value = indicatorMilestone.Where(x => x.MilestoneID == ((int)indicatorMilestone.Min(r => r.MilestoneID))).Select(x => x.Achieved).First();
ws.Cells["F46"].Value = indicatorMilestone.Where(x => x.MilestoneID == ((int)indicatorMilestone.Min(r => r.MilestoneID)) + 1).Select(x => x.Achieved).First();
ws.Cells["G46"].Value = indicatorMilestone.Where(x => x.MilestoneID == ((int)indicatorMilestone.Min(r => r.MilestoneID)) + 2).Select(x => x.Achieved).First();
//Export to excel
}
else
{
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
ExcelFile ef = ExcelFile.Load(Server.MapPath("~/SpreadsheetTemplate/Logframe_Template.xlsx"));
ExcelWorksheet ws = ef.Worksheets[0];
ws.Cells["B2"].Value = resultViewModel.ProjectHeader.Title;
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats";
Response.AddHeader("Content-Disposition", "attachment; filename=" + "Logframe-" + projectOutput.ProjectID + ".xlsx");
MemoryStream ms = new MemoryStream();
ef.SaveXlsx(ms);
ms.WriteTo(Response.OutputStream);
Response.End();
}
Response.End();
}
