Create Column Chart Accoring to Data Information in Excel with C#

MS Excel provides several kinds of charts with users. Chart can display data information more intuitively to make viewers learn data more clearly. In this post, I will create a column chart according to specified data by using C#.

Column Chart is often used to present data changes during one period, for example, one year and comparison between items.

In my example, I prepare one Excel file. This file includes four items, product no., quantity, unite price and total sales. I will create a column chart to display information about quantity and unit price according to product no. There are several types of column chart. And I choose Column 3D Clustered chart.

Data

At the beginning, add dll file of Spire.XLS for .NET, a C# .NET Excel component used in this method, as reference because I use it to achieve my needing.

Then, start with creating.

Step 1: load my file from computer and initialize worksheet.

Step 2: add chart in worksheet and select chart data range.

Step 3: set chart location in worksheet.

Step 4: set chart type as column 3D clustered chart.

Step 5: set chart title, axes titles and legend location. Also, format them.

Step 6: save and launch data.

Coding:

using System;
using Spire.Xls;
using Spire.Xls.Charts;

namespace Column_Chart
{
    class Charts
    {
        static void Main(string[] args)
        {

            //Load File and Initialize Worksheet
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@”D:\work\My Documents\Sales Report.xlsx”, ExcelVersion.Version2010);
            Worksheet sheet = workbook.Worksheets[0];

            //Add Chart and Select Chart Range
            Chart chart = sheet.Charts.Add();
            chart.DataRange = sheet.Range[“B2:C9”];
            chart.SeriesDataFromRange = false;

            //Set Chart Location
            chart.LeftColumn = 1;
            chart.TopRow = 12;
            chart.RightColumn = 10;
            chart.BottomRow = 31;

            //Get Chart Type
            chart.ChartType = ExcelChartType.Column3DClustered;

            //Chart Title and Axes Title
            chart.ChartTitle = “Sales Info”;
            chart.ChartTitleArea.Font.FontName = “Calibri”;
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;
            chart.PrimaryCategoryAxis.Title = “Product No”;
            chart.PrimaryCategoryAxis.Font.FontName = “Calibri”;
            chart.PrimaryCategoryAxis.Font.Size = 11;
            chart.PrimaryCategoryAxis.Font.IsBold = true;
            chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
            chart.PrimaryValueAxis.Title = “Unit Price and Quantity”;
            chart.PrimaryValueAxis.HasMajorGridLines = false;
            chart.PrimaryValueAxis.Font.FontName = “Calibri”;
            chart.PrimaryValueAxis.Font.Size = 11;
            chart.PrimaryValueAxis.MinValue = 100;
            chart.PrimaryValueAxis.TitleArea.IsBold = true;
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;

            //Legend Position
            chart.Legend.Position = LegendPositionType.Right;

            //Save and Launch File
            workbook.SaveToFile(“Column Chart.xlsx”, ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

1 thought on “Create Column Chart Accoring to Data Information in Excel with C#”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s