Articles in this section
Category / Section

How to Create Treemap Chart in Excel Worksheet in C#, VB.NET?

4 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

This article explains how to create a treemap chart in an Excel worksheet using C# and VB.NET.

Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

VB.NET

Imports Syncfusion.XlsIO

Use the following code snippet for creating a treemap chart in an Excel worksheet.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Create(1);
    IWorksheet worksheet = workbook.Worksheets[0];

    //Enter sample data
    worksheet.Range["A1"].Text = "Category";
    worksheet.Range["B1"].Text = "SubCategory";
    worksheet.Range["C1"].Text = "Value";

    worksheet.Range["A2"].Text = "Fruit";
    worksheet.Range["B2"].Text = "Apple";
    worksheet.Range["C2"].Number = 50;

    worksheet.Range["A3"].Text = "Fruit";
    worksheet.Range["B3"].Text = "Banana";
    worksheet.Range["C3"].Number = 30;

    worksheet.Range["A4"].Text = "Vegetable";
    worksheet.Range["B4"].Text = "Carrot";
    worksheet.Range["C4"].Number = 40;

    worksheet.Range["A5"].Text = "Vegetable";
    worksheet.Range["B5"].Text = "Broccoli";
    worksheet.Range["C5"].Number = 25;

    //Add chart to worksheet
    IChartShape chart = worksheet.Charts.Add();

    //Set chart type to Treemap
    chart.ChartType = ExcelChartType.TreeMap;

    //Set chart data range
    chart.DataRange = worksheet.Range["A1:C5"];
    chart.IsSeriesInRows = false;

    //Set chart title
    chart.ChartTitle = "Treemap Chart";

    //Positioning the chart in the worksheet
    chart.TopRow = 8;
    chart.LeftColumn = 1;
    chart.BottomRow = 23;
    chart.RightColumn = 8;

    #region Save
    //Saving the workbook
    FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
    workbook.SaveAs(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();
}

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    'Enter sample data
    worksheet.Range("A1").Text = "Category"
    worksheet.Range("B1").Text = "SubCategory"
    worksheet.Range("C1").Text = "Value"

    worksheet.Range("A2").Text = "Fruit"
    worksheet.Range("B2").Text = "Apple"
    worksheet.Range("C2").Number = 50

    worksheet.Range("A3").Text = "Fruit"
    worksheet.Range("B3").Text = "Banana"
    worksheet.Range("C3").Number = 30

    worksheet.Range("A4").Text = "Vegetable"
    worksheet.Range("B4").Text = "Carrot"
    worksheet.Range("C4").Number = 40

    worksheet.Range("A5").Text = "Vegetable"
    worksheet.Range("B5").Text = "Broccoli"
    worksheet.Range("C5").Number = 25

    'Add chart to worksheet
    Dim chart As IChartShape = worksheet.Charts.Add()

    'Set chart type to Treemap
    chart.ChartType = ExcelChartType.TreeMap

    'Set chart data range
    chart.DataRange = worksheet.Range("A1:C5")
    chart.IsSeriesInRows = False

    'Set chart title
    chart.ChartTitle = "Treemap Chart"

    'Positioning the chart in the worksheet
    chart.TopRow = 8
    chart.LeftColumn = 1
    chart.BottomRow = 23
    chart.RightColumn = 8

    'Saving the workbook
    workbook.SaveAs("Output.xlsx")
End Using  

You can get the complete sample for creating a treemap chart in an Excel worksheet from here.

I hope you enjoyed learning how to create a treemap chart in an Excel worksheet using C# and VB.NET in XlsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied