Search results

Excel Export in ASP.NET Core Gantt control

Gantt supports client-side exporting, which allows you to export its data to the Excel and CSV formats. Use the excelExport and csvExport methods for exporting. To enable Excel export in the Gantt, set the AllowExcelExport to true.

tagHelper
excelExport.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" toolbar="@(new List<string>() { "ExcelExport", "CsvExport" })"
    toolbarClick="toolbarClick" allowExcelExport="true">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>
<script>
    function toolbarClick(args) {
        var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            if (args.item.id === 'GanttContainer_excelexport') {
                gantt.excelExport();
            } else if(args.item.id === 'GanttContainer_csvexport'){
                gantt.csvExport();
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

Multiple Gantt exporting

In Gantt, the Excel export provides support to export multiple Gantt data in new sheet or same sheet.

Same sheet

The Excel export provides support to export multiple Gantt data in the same sheet. To export in same sheet, define multipleExport.type as AppendToSheet in ExcelExportProperties. You can also provide blank rows between exported multiple Gantt data. These blank rows count can be defined using multipleExport.blankRows.

tagHelper
sameSheet.cs
<ejs-gantt id='GanttContainer1' dataSource="ViewBag.FirstData" height="280px" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport"})" toolbarClick="toolbarClick" treeColumnIndex="1" projectStartDate="03/31/2019" projectEndDate="04/14/2019">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>

<ejs-gantt id='GanttContainer2' dataSource="ViewBag.FirstData" height="250px" allowExcelExport="true" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>

<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer1_excelexport') {
            var firstGantt = document.getElementById("GanttContainer1").ej2_instances[0];
            var secondGantt = document.getElementById("GanttContainer2").ej2_instances[0];
            var appendExcelExportProperties = {
                multipleExport: { type: 'AppendToSheet', blankRows: 2 }
            };
            var firstGanttExport = firstGantt.excelExport(appendExcelExportProperties, true);
            firstGanttExport.then((fData) => {
                secondGantt.excelExport(appendExcelExportProperties, false, fData);
            });
        } 
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

By default, multipleExport.blankRows value is 5.

New sheet

The Excel exporting provides support to export multiple Gantt in new sheet. To export in new sheet, define multipleExport.type as NewSheet in ExcelExportProperties.

tagHelper
newSheet.cs
<ejs-gantt id='GanttContainer1' dataSource="ViewBag.FirstData" height="280px" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport"})" toolbarClick="toolbarClick" treeColumnIndex="1" projectStartDate="03/31/2019" projectEndDate="04/14/2019">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>

<ejs-gantt id='GanttContainer2' dataSource="ViewBag.FirstData" height="250px" allowExcelExport="true" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>

<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer1_excelexport') {
            var firstGantt = document.getElementById("GanttContainer1").ej2_instances[0];
            var secondGantt = document.getElementById("GanttContainer2").ej2_instances[0];
            var appendExcelExportProperties = {
                multipleExport: { type: 'NewSheet' }
            };
            var firstGanttExport = firstGantt.excelExport(appendExcelExportProperties, true);
            firstGanttExport.then((fData) => {
                secondGantt.excelExport(appendExcelExportProperties, false, fData);
            });
        } 
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

Customize the Excel export

Gantt Excel export allows the users to customize the exported document based on requirement.

Export hidden columns

In Gantt, the Excel export provides an option to export hidden columns by defining includeHiddenColumn as true.

tagHelper
exportHiddenColumns.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" allowExcelExport="true" 
    toolbar="@(new List<string>() { "ExcelExport", "CsvExport" })" toolbarClick="toolbarClick" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
    <e-gantt-columns>
        <e-gantt-column field="TaskId" headerText="Task ID" width="150" textAlign="Left"></e-gantt-column>
        <e-gantt-column field="TaskName" headerText="Task Name" width="250"></e-gantt-column>
        <e-gantt-column field="StartDate" headerText="StartDate" width="250" visible="false"></e-gantt-column>
        <e-gantt-column field="Duration" headerText="Duration" width="150"></e-gantt-column>
        <e-gantt-column field="Progress" headerText="Progress" width="250"></e-gantt-column>
    </e-gantt-columns>
</ejs-gantt>
<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer_excelexport') {
            var excelExportProperties = {
                includeHiddenColumn: true
            };
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
                gantt.excelExport(excelExportProperties);
         } else if (args.item.id === 'GanttContainer_csvexport') {
            var excelExportProperties = {
                includeHiddenColumn: true
            };
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.csvExport(excelExportProperties);
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

Show or hide columns on exported Excel

In Gantt, while exporting, you can show a hidden column or hide a visible column using the ToolbarClick and ExcelExportComplete events.

In the ToolbarClick event, using the args.item.id property, you can show or hide columns by setting the Columns.Visible property to true or false respectively.

Similarly, in the excelExportComplete event, you can revert the columns visibility back to the previous state.

tagHelper
showHideColumn.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport", "CsvExport" })" toolbarClick="toolbarClick" excelExportComplete="excelExportComplete" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
    <e-gantt-columns>
        <e-gantt-column field="TaskId" headerText="Task ID" width="150" textAlign="Left"></e-gantt-column>
        <e-gantt-column field="TaskName" headerText="Task Name" width="250"></e-gantt-column>
        <e-gantt-column field="StartDate" headerText="StartDate" width="250" visible="false"></e-gantt-column>
        <e-gantt-column field="Duration" headerText="Duration" width="150"></e-gantt-column>
        <e-gantt-column field="Progress" headerText="Progress" width="250"></e-gantt-column>
    </e-gantt-columns>
</ejs-gantt>
<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer_excelexport') {
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.treeGrid.grid.columns[0].visible = true;
            gantt.treeGrid.grid.columns[3].visible = false;
            gantt.excelExport();
        } else if(args.item.id === 'GanttContainer_csvexport'){
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.treeGrid.grid.columns[0].visible = true;
            gantt.treeGrid.grid.columns[3].visible = false;
            gantt.csvExport();
        }
    }
     function excelExportComplete(args) {
        var gantt = document.getElementById("GanttContainer").ej2_instances[0];
        gantt.treeGrid.grid.columns[0].visible = false;
        gantt.treeGrid.grid.columns[3].visible = true;
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

Cell formatting during export

In Gantt, you can customize the TreeGrid cells in the exported document using the ExcelQueryCellInfo event. In this event, you can format the TreeGrid cells of exported Excel and CSV documents based on the required condition.

In the following sample, the background color has been customized for TaskId column in the exported Excel using the args.style and backColor properties.

tagHelper
cellFormat.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport"})" toolbarClick="toolbarClick" excelQueryCellInfo="excelQueryCellInfo" queryCellInfo="queryCellInfo" queryTaskbarInfo="queryTaskbarInfo" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
    <e-gantt-columns>
        <e-gantt-column field="TaskId" headerText="Task ID" textAlign="Left" width="100" visible="false"></e-gantt-column>
        <e-gantt-column field="TaskName" headerText="Task Name" width="150"></e-gantt-column>
        <e-gantt-column field="Progress" width="150"></e-gantt-column>
        <e-gantt-column field="StartDate" width="150"></e-gantt-column>
        <e-gantt-column field="Duration" width="150"></e-gantt-column>
    </e-gantt-columns>
    <e-gantt-labelSettings taskLabel="${Progress}%"></e-gantt-labelSettings>
    <e-gantt-splittersettings columnIndex=3></e-gantt-splittersettings>
</ejs-gantt>
<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer_excelexport') {
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.excelExport();
        }
    }
    function excelQueryCellInfo(args)  {
        if(args.column.field == 'Progress'){
            if(args.value > 80) {
                args.style = { backColor: '#A569BD' };
            }
            else if(args.value < 20) {
                args.style = { backColor: '#F08080' };
            }
        }
    }
    function queryTaskbarInfo(args) {
        if (args.data.Progress > 80) {
            args.progressBarBgColor = "#6C3483";
            args.taskbarBgColor = args.taskbarBorderColor = "#A569BD";
        } else if (args.data.Progress < 20) {
            args.progressBarBgColor = "#CD5C5C";
            args.taskbarBgColor = args.taskbarBorderColor = "#F08080";
        }
    }
    function queryCellInfo(args)  {
        if(args.column.field == 'Progress'){
            if(args.data.Progress > 80) {
                args.cell.style.backgroundColor  = '#A569BD';
            }
            else if(args.data.Progress < 20) {
                args.cell.style.backgroundColor  = '#F08080';
            }
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

Theme

The Excel export also provides an option to include custom theme for exported Excel document.

To apply theme in exported Excel, define the theme in ExcelExportProperties.

tagHelper
theme.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport"})" toolbarClick="toolbarClick" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>
<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer_excelexport') {
            var excelExportProperties = {
                theme:
                        {
                            header: { fontName: 'Segoe UI', fontColor: '#666666' },
                            record: { fontName: 'Segoe UI', fontColor: '#666666' }
                        }
            };
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.excelExport(excelExportProperties);
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

By default, material theme is applied to the exported Excel document.

The Excel export also allows users to include header and footer contents to the exported Excel document.

tagHelper
headerFooter.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport"})" toolbarClick="toolbarClick" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>
<script>
    function toolbarClick(args) {
        if (args.item.id === 'GanttContainer_excelexport') {
            var excelExportProperties = {
                header: {
                            headerRows: 7,
                            rows: [
                                { cells: [{ colSpan: 4, value: "Northwind Traders", style: { fontColor: '#C67878', fontSize: 20, hAlign: 'Center', bold: true, } }] },
                                { cells: [{ colSpan: 4, value: "2501 Aerial Center Parkway", style: { fontColor: '#C67878', fontSize: 15, hAlign: 'Center', bold: true, } }] },
                                { cells: [{ colSpan: 4, value: "Suite 200 Morrisville, NC 27560 USA", style: { fontColor: '#C67878', fontSize: 15, hAlign: 'Center', bold: true, } }] },
                                { cells: [{ colSpan: 4, value: "Tel +1 888.936.8638 Fax +1 919.573.0306", style: { fontColor: '#C67878', fontSize: 15, hAlign: 'Center', bold: true, } }] },
                                { cells: [{ colSpan: 4, hyperlink: { target: 'https://www.northwind.com/', displayText: 'www.northwind.com' }, style: { hAlign: 'Center' } }] },
                                { cells: [{ colSpan: 4, hyperlink: { target: 'mailto:support@northwind.com' }, style: { hAlign: 'Center' } }] },
                            ]
                        },
                        footer: {
                            footerRows: 4,
                            rows: [
                                { cells: [{ colSpan: 4, value: "Thank you for your business!", style: { hAlign: 'Center', bold: true } }] },
                                { cells: [{ colSpan: 4, value: "!Visit Again!", style: { hAlign: 'Center', bold: true } }] }
                            ]
                        },
            };
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.excelExport(excelExportProperties);
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}

File name for exported document

You can set the required file name for the exported document by defining the fileName property in ExcelExportProperties.

tagHelper
fileName.cs
<ejs-gantt id='GanttContainer' dataSource="ViewBag.DataSource" allowExcelExport="true"
    toolbar="@(new List<string>() { "ExcelExport", "CsvExport"})" toolbarClick="toolbarClick" treeColumnIndex="1">
    <e-gantt-taskfields id="TaskId" name="TaskName" startDate="StartDate" duration="Duration" progress="Progress" child="SubTasks">
    </e-gantt-taskfields>
</ejs-gantt>
<script>
    function toolbarClick(args) { 
        if (args.item.id === 'GanttContainer_excelexport') {
            var excelExportProperties = {
                fileName:"Gantt.xlsx"
            };
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.excelExport(excelExportProperties);
        } else if (args.item.id === 'GanttContainer_csvexport'){
            var excelExportProperties = {
                fileName:"Gantt.csv"
            };
            var gantt = document.getElementById("GanttContainer").ej2_instances[0];
            gantt.csvExport(excelExportProperties);
        }
    }
</script>
public IActionResult Index()
{
    ViewBag.DataSource = FirstData();
    return View();
}
public static List<GanttDataSource> FirstData()
{
    List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

    GanttDataSource Record1 = new GanttDataSource()
    {
        TaskId = 1,
        TaskName = "Project initiation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child1 = new GanttDataSource()
    {
        TaskId = 2,
        TaskName = "Identify site location",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 90
    };
    GanttDataSource Child2 = new GanttDataSource()
    {
        TaskId = 3,
        TaskName = "Perform soil test",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 40
    };
    GanttDataSource Child3 = new GanttDataSource()
    {
        TaskId = 4,
        TaskName = "Soil test approval",
        StartDate = new DateTime(2019, 04, 02),
        Duration = 4,
        Progress = 10
    };
    Record1.SubTasks.Add(Child1);
    Record1.SubTasks.Add(Child2);
    Record1.SubTasks.Add(Child3);

    GanttDataSource Record2 = new GanttDataSource()
    {
        TaskId = 5,
        TaskName = "Project estimation",
        StartDate = new DateTime(2019, 04, 02),
        EndDate = new DateTime(2019, 04, 21),
        SubTasks = new List<GanttDataSource>(),
    };
    GanttDataSource Child4 = new GanttDataSource()
    {
        TaskId = 6,
        TaskName = "Develop floor plan for estimation",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 85
    };
    GanttDataSource Child5 = new GanttDataSource()
    {
        TaskId = 7,
        TaskName = "List materials",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 15
    };
    GanttDataSource Child6 = new GanttDataSource()
    {
        TaskId = 8,
        TaskName = "Estimation approval",
        StartDate = new DateTime(2019, 04, 04),
        Duration = 3,
        Progress = 70
    };
    Record2.SubTasks.Add(Child4);
    Record2.SubTasks.Add(Child5);
    Record2.SubTasks.Add(Child6);

    GanttDataSourceCollection.Add(Record1);
    GanttDataSourceCollection.Add(Record2);

    return GanttDataSourceCollection;
}
public class GanttDataSource
{
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int? Duration { get; set; }
    public int Progress { get; set; }
    public List<GanttDataSource> SubTasks { get; set; }
}