How to Create Column Chart in Excel
Column chart also known as bar chart is a professional way to present many different types of data. It is a chart with rectangular bars of lengths usually proportional to the magnitudes or frequencies of what they represent. The bars can be horizontally or vertically oriented. Usually column chart is vertically oriented bars and bar chart is with horizontally oriented bars. In column charts, categories are typically organized along the horizontal axis and values along the vertical axis. Column chart in Excel is useful for showing data changes over a period of time or for illustrating comparisons among items. In this post, you will be introduced a solution of using C# to create column chart in Excel.
How to Create Column chart in Excel via C#?
In the former post we introduced how to use Spire.XLS to create line chart in Excel. Here we can still use Spire.XLS to create column chart in Excel via C#.
Download Spire.XLS Here
Make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the simple steps below to create column chart in Excel.
Step 1 Create Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using Spire.Xls;
namespace columnchart
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Create Excel Spreadsheet
We can use Spire.XLs to create an Excel spreadsheet for later usage on creating column chart.
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0];
//Set the name of the chart
sheet.Name = "Chart data";
//Set whether the grid line is visible
sheet.GridLinesVisible = false;
Step 3 Write Data in Excel Spreadsheet
Write some data into the Excel spreadsheet. By using Spire.XLS we can set styles, borders and number format.
//Writes chart data country
sheet.Range["A1"].Value = "Country";
sheet.Range["A2"].Value = "Cuba";
sheet.Range["A3"].Value = "Mexico";
sheet.Range["A4"].Value = "France";
sheet.Range["A5"].Value = "German";
//Jun
sheet.Range["B1"].Value = "Jun";
sheet.Range["B2"].NumberValue = 6000;
sheet.Range["B3"].NumberValue = 8000;
sheet.Range["B4"].NumberValue = 9000;
sheet.Range["B5"].NumberValue = 8500;
//Aug
sheet.Range["C1"].Value = "Aug";
sheet.Range["C2"].NumberValue = 3000;
sheet.Range["C3"].NumberValue = 2000;
sheet.Range["C4"].NumberValue = 2300;
sheet.Range["C5"].NumberValue = 4200;
//Style
sheet.Range["A1:C1"].Style.Font.IsBold = true;
sheet.Range["A2:C2"].Style.KnownColor = ExcelColors.LightYellow;
sheet.Range["A3:C3"].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range["A4:C4"].Style.KnownColor = ExcelColors.LightOrange;
sheet.Range["A5:C5"].Style.KnownColor = ExcelColors.LightTurquoise;
//Border
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
//Set number format
sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
Step 4 Create Column Chart in Excel
Create column chart in Excel according to the data information we import above. Spire.XLS allows us to set chart position, title and other parameters.
//Add a new chart worsheet to workbook
Chart chart = sheet.Charts.Add(ExcelChartType.Column3DClustered);
//Set region of chart data
chart.DataRange = sheet.Range["A1:C5"];
chart.SeriesDataFromRange = false;
//Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 6;
chart.RightColumn = 11;
chart.BottomRow = 29;
//Chart title
chart.ChartTitle = "Sales market by country";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
//Category title
chart.PrimaryCategoryAxis.Title = "Country";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
//Value title
chart.PrimaryValueAxis.Title = "Sales(in Dollars)";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.MinValue = 1000;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
foreach (Spire.Xls.Charts.ChartSerie cs in chart.Series)
{
//Set vary color of the chart
cs.Format.Options.IsVaryColor = true;
//Set the value is visible on the chart
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
//Legend position
chart.Legend.Position = LegendPositionType.Top;
Step 5 Save and Preview
//Save the file
workbook.SaveToFile("Sample.xls");
//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
Effective Screenshot:
How to Create Column chart in Excel via C#?
In the former post we introduced how to use Spire.XLS to create line chart in Excel. Here we can still use Spire.XLS to create column chart in Excel via C#.
Download Spire.XLS Here
Make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the simple steps below to create column chart in Excel.
Step 1 Create Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using Spire.Xls;
namespace columnchart
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Create Excel Spreadsheet
We can use Spire.XLs to create an Excel spreadsheet for later usage on creating column chart.
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0];
//Set the name of the chart
sheet.Name = "Chart data";
//Set whether the grid line is visible
sheet.GridLinesVisible = false;
Step 3 Write Data in Excel Spreadsheet
Write some data into the Excel spreadsheet. By using Spire.XLS we can set styles, borders and number format.
//Writes chart data country
sheet.Range["A1"].Value = "Country";
sheet.Range["A2"].Value = "Cuba";
sheet.Range["A3"].Value = "Mexico";
sheet.Range["A4"].Value = "France";
sheet.Range["A5"].Value = "German";
//Jun
sheet.Range["B1"].Value = "Jun";
sheet.Range["B2"].NumberValue = 6000;
sheet.Range["B3"].NumberValue = 8000;
sheet.Range["B4"].NumberValue = 9000;
sheet.Range["B5"].NumberValue = 8500;
//Aug
sheet.Range["C1"].Value = "Aug";
sheet.Range["C2"].NumberValue = 3000;
sheet.Range["C3"].NumberValue = 2000;
sheet.Range["C4"].NumberValue = 2300;
sheet.Range["C5"].NumberValue = 4200;
//Style
sheet.Range["A1:C1"].Style.Font.IsBold = true;
sheet.Range["A2:C2"].Style.KnownColor = ExcelColors.LightYellow;
sheet.Range["A3:C3"].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range["A4:C4"].Style.KnownColor = ExcelColors.LightOrange;
sheet.Range["A5:C5"].Style.KnownColor = ExcelColors.LightTurquoise;
//Border
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
//Set number format
sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
Step 4 Create Column Chart in Excel
Create column chart in Excel according to the data information we import above. Spire.XLS allows us to set chart position, title and other parameters.
//Add a new chart worsheet to workbook
Chart chart = sheet.Charts.Add(ExcelChartType.Column3DClustered);
//Set region of chart data
chart.DataRange = sheet.Range["A1:C5"];
chart.SeriesDataFromRange = false;
//Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 6;
chart.RightColumn = 11;
chart.BottomRow = 29;
//Chart title
chart.ChartTitle = "Sales market by country";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
//Category title
chart.PrimaryCategoryAxis.Title = "Country";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
//Value title
chart.PrimaryValueAxis.Title = "Sales(in Dollars)";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.MinValue = 1000;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
foreach (Spire.Xls.Charts.ChartSerie cs in chart.Series)
{
//Set vary color of the chart
cs.Format.Options.IsVaryColor = true;
//Set the value is visible on the chart
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
//Legend position
chart.Legend.Position = LegendPositionType.Top;
Step 5 Save and Preview
//Save the file
workbook.SaveToFile("Sample.xls");
//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
Effective Screenshot:
More about Spire.XLS
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more...
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more...