Learn how to Create Gantt Chart in Excel using C#
Created
April 16, 2025 01:22
-
-
Save aspose-com-gists/f5f87aab2f6b9e6b63bc6f3ffdab8613 to your computer and use it in GitHub Desktop.
Create Gantt Chart in Excel using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Cells; | |
using Aspose.Cells.Charts; | |
// Load the Excel file | |
Workbook workbook = new Workbook("sample-data.xlsx"); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add Gantt Chart | |
int chartIndex = sheet.Charts.Add(ChartType.BarStacked, 7, 0, 30, 10); | |
Chart chart = sheet.Charts[chartIndex]; | |
chart.Title.Text = "Gantt Chart"; | |
// Add offset and duration as series | |
chart.NSeries.Add("D2:D6", true); // Start Offset | |
chart.NSeries.Add("C2:C6", true); // Duration | |
// Set task names as categories | |
chart.NSeries.CategoryData = "A2:A6"; | |
// Make offset bars invisible | |
chart.NSeries[0].Area.Formatting = FormattingType.None; | |
// Style duration bars | |
chart.NSeries[1].Area.Formatting = FormattingType.Custom; | |
chart.NSeries[1].Area.ForegroundColor = Color.SteelBlue; | |
chart.NSeries[1].DataLabels.ShowValue = true; | |
// Axis settings | |
chart.CategoryAxis.IsPlotOrderReversed = true; | |
chart.CategoryAxis.Title.Text = "Tasks"; | |
chart.ValueAxis.Title.Text = "Days from Start"; | |
chart.ValueAxis.MajorGridLines.IsVisible = true; | |
// Hide legend | |
chart.ShowLegend = false; | |
// Resize chart (optional) | |
chart.ChartObject.Width = 700; | |
chart.ChartObject.Height = 400; | |
// Save the result | |
workbook.Save("GanttChartResult.xlsx", SaveFormat.Xlsx); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Cells; | |
using Aspose.Cells.Charts; | |
// Initialize workbook and worksheet | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
sheet.Name = "Gantt Data"; | |
// Sample Task Data with Dates | |
DateTime projectStart = new DateTime(2025, 4, 1); | |
// Headers | |
sheet.Cells["A1"].PutValue("Task"); | |
sheet.Cells["B1"].PutValue("Start Date"); | |
sheet.Cells["C1"].PutValue("Duration (Days)"); | |
sheet.Cells["D1"].PutValue("Start Offset"); | |
sheet.Cells["E1"].PutValue("End Date"); | |
// Data | |
string[,] tasks = { | |
{ "Task 1", "2025-04-01", "3" }, | |
{ "Task 2", "2025-04-03", "6" }, | |
{ "Task 3", "2025-04-08", "10" }, | |
{ "Task 4", "2025-04-18", "3" }, | |
{ "Task 5", "2025-04-21", "5" } | |
}; | |
for (int i = 0; i < tasks.GetLength(0); i++) | |
{ | |
int row = i + 1 + 1; // Start from row 2 | |
sheet.Cells[row, 0].PutValue(tasks[i, 0]); // Task Name | |
sheet.Cells[row, 1].PutValue(DateTime.Parse(tasks[i, 1])); // Start Date | |
sheet.Cells[row, 2].PutValue(int.Parse(tasks[i, 2])); // Duration | |
// Offset: = StartDate - ProjectStartDate | |
sheet.Cells[row, 3].Formula = $"=B{row + 1} - DATE(2025,4,1)"; | |
sheet.Cells[row, 4].Formula = $"=B{row + 1} + C{row + 1}"; | |
} | |
// Autofit | |
sheet.AutoFitColumns(); | |
// Add a chart | |
int chartIndex = sheet.Charts.Add(ChartType.BarStacked, 10, 0, 30, 10); | |
Chart chart = sheet.Charts[chartIndex]; | |
chart.Title.Text = "Project Gantt Chart"; | |
// Add Series: Offset (invisible), Duration (visible) | |
chart.NSeries.Add("D2:D6", true); // Offset | |
chart.NSeries.Add("C2:C6", true); // Duration | |
// Set category labels | |
chart.NSeries.CategoryData = "A2:A6"; | |
// Make Offset series transparent | |
chart.NSeries[0].Area.Formatting = FormattingType.None; | |
// Color each task bar differently | |
Series durationSeries = chart.NSeries[1]; | |
for (int i = 0; i < durationSeries.Points.Count; i++) | |
{ | |
durationSeries.Points[i].Area.ForegroundColor = Color.FromArgb(255, 100 + i * 30, 150); // Gradient color variation | |
} | |
// Show data labels | |
chart.NSeries[1].DataLabels.ShowValue = true; | |
// Axis Settings | |
chart.CategoryAxis.Title.Text = "Tasks"; | |
chart.CategoryAxis.IsPlotOrderReversed = true; | |
chart.ValueAxis.Title.Text = "Days from Project Start"; | |
chart.ValueAxis.MajorGridLines.IsVisible = true; | |
chart.ValueAxis.MinValue = 0; | |
chart.ValueAxis.MajorUnit = 5; | |
// Remove Legend | |
chart.ShowLegend = false; | |
// Resize chart | |
chart.ChartObject.Width = 700; | |
chart.ChartObject.Height = 400; | |
// Export to Excel | |
workbook.Save("CustomizedGanttChart.xlsx", SaveFormat.Xlsx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment