Upcoming SlideShare
×

# C# excel bar chart

623
-1

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
623
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### C# excel bar chart

1. 1. How to Use C# Create Excel bar ChartWhy Create Excel Bar Chart?For numerical comparisons, bar chart is a classic solution. Bar chart (graph) is a chart withrectangular bars with lengths proportional to the values that they represent. The bars can be plottedvertically or horizontally. A bar chart is very useful if you are trying to record certain informationwhether it is continuous or not continuous data. In bar charts, a class or group can have a singlecategory of data, or they can be broken down further into multiple categories for greater depth ofanalysis. And in Microsoft Excel, it is the same useful!How to Use C# Create Excel Bar Chart?Spire.XLS for .NET allows user to use C# create Excel bar chart. Use C# to create Excel bar chartcould be as easy as in Microsoft Excel. The whole process is almost the same such as write chartdata, set region/position of chart, Write chart title, chart data information, etc. The difference is weneed fill these data in C# application with codes around. Below is a sample for C# code used tocreate Excel bar charts. Download Spire.XLS (or Spire.Office) with .NET framework 2.0 (orabove) together and use the code to create Excel Bar Chart right now.C# Excel Bar Chart:using Spire.Xls;using Spire.Xls.Charts;namespace Spire.Xls.Sample{ class program { static void Main() { Application.Run(new Form1()); } private void btnRun_Click(object sender, System.EventArgs e) { Workbook workbook = new Workbook(); //Initailize worksheet workbook.CreateEmptySheets(1); Worksheet sheet = workbook.Worksheets[0]; sheet.Name = "Chart data"; sheet.GridLinesVisible = false; //Writes chart data CreateChartData(sheet); //Add a new chart worsheet to workbook
2. 2. Chart chart = sheet.Charts.Add(); //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; if (checkBox1.Checked) { chart.ChartType = ExcelChartType.Bar3DClustered; } else { chart.ChartType = ExcelChartType.BarClustered; }//Chart title chart.ChartTitle = "Sales market by country"; chart.ChartTitleArea.IsBold = true; chart.ChartTitleArea.Size = 12; chart.PrimaryCategoryAxis.Title = "Country"; chart.PrimaryCategoryAxis.Font.IsBold = true; chart.PrimaryCategoryAxis.TitleArea.IsBold = true; chart.PrimaryCategoryAxis.TitleArea.TextRotationAngle = 90; chart.PrimaryValueAxis.Title = "Sales(in Dollars)"; chart.PrimaryValueAxis.HasMajorGridLines = false;chart.PrimaryValueAxis.MinValue = 1000; chart.PrimaryValueAxis.TitleArea.IsBold = true;foreach (Charts.ChartSerie cs in chart.Series){ cs.Format.Options.IsVaryColor = true; cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;}
3. 3. chart.Legend.Position = LegendPositionType.Top; workbook.SaveToFile("Sample.xls"); ExcelDocViewer(workbook.FileName); } private void CreateChartData(Worksheet sheet) { //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; //Jun 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;
4. 4. 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; sheet.Range["B2:C5"].Style.NumberFormat = ""\$"#,##0"; } private void ExcelDocViewer( string fileName ) { try { System.Diagnostics.Process.Start(fileName); } catch{} } private void btnAbout_Click(object sender, System.EventArgs e) { Close(); } }}After running your application with the code above, you will find an Excel bar Chart is created.