The sample demonstrates how to create a pie chart in an excel workbook.
private void btnRun_Click(object sender, System.EventArgs e) { Workbook workbook = new Workbook(); //Initialize worksheet workbook.CreateEmptySheets(1); Worksheet sheet = workbook.Worksheets[0]; sheet.Name = "Chart data"; sheet.GridLinesVisible = false; //Add a new chart worsheet to workbook Chart chart = null; if (checkBox1.Checked) { chart = sheet.Charts.Add(ExcelChartType.Pie3D); } else { chart = sheet.Charts.Add(ExcelChartType.Pie); } CreateChartData(sheet); CreateChart(sheet, chart); workbook.SaveToFile("Sample.xls"); ExcelDocViewer(workbook.FileName); } private void CreateChart(Worksheet sheet, Chart chart) { //Set region of chart data chart.DataRange = sheet.Range["B2:B5"]; chart.SeriesDataFromRange = false; //Set position of chart chart.LeftColumn = 1; chart.TopRow = 6; chart.RightColumn = 9; chart.BottomRow = 25; //Chart title chart.ChartTitle = "Sales by year"; chart.ChartTitleArea.IsBold = true; chart.ChartTitleArea.Size = 12; Charts.ChartSerie cs = chart.Series[0]; cs.CategoryLabels = sheet.Range["A2:A5"]; cs.Values = sheet.Range["B2:B5"]; cs.DataFormat.ShowActiveValue = true; } private void CreateChartData(Worksheet sheet) { //Country sheet.Range["A1"].Value = "Year"; sheet.Range["A2"].Value = "2002"; sheet.Range["A3"].Value = "2003"; sheet.Range["A4"].Value = "2004"; sheet.Range["A5"].Value = "2005"; //Jun sheet.Range["B1"].Value = "Sales"; sheet.Range["B2"].NumberValue = 4000; sheet.Range["B3"].NumberValue = 6000; sheet.Range["B4"].NumberValue = 7000; sheet.Range["B5"].NumberValue = 8500; //Style sheet.Range["A1:B1"].Style.Font.IsBold = true; sheet.Range["A2:B2"].Style.KnownColor = ExcelColors.LightYellow; sheet.Range["A3:B3"].Style.KnownColor = ExcelColors.LightGreen1; sheet.Range["A4:B4"].Style.KnownColor = ExcelColors.LightOrange; sheet.Range["A5:B5"].Style.KnownColor = ExcelColors.LightTurquoise; //Border sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128); sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin; sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128); sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin; sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128); sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin; sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128); sheet.Range["A1:B5"].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 Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click Dim workbook As Workbook = New Workbook() 'Initialize worksheet workbook.CreateEmptySheets(1) Dim sheet As Worksheet = workbook.Worksheets(0) sheet.Name = "Chart data" sheet.GridLinesVisible = False 'Add a new chart worsheet to workbook Dim chart As Chart = Nothing If checkBox1.Checked Then chart = sheet.Charts.Add(ExcelChartType.Pie3D) Else chart = sheet.Charts.Add(ExcelChartType.Pie) End If CreateChartData(sheet) CreateChart(sheet, chart) workbook.SaveToFile("Sample.xls") ExcelDocViewer(workbook.FileName) End Sub Private Sub CreateChart(ByVal sheet As Worksheet, ByVal chart As Chart) 'Set region of chart data chart.DataRange = sheet.Range("B2:B5") chart.SeriesDataFromRange = False 'Set position of chart chart.LeftColumn = 1 chart.TopRow = 6 chart.RightColumn = 9 chart.BottomRow = 25 'Chart title chart.ChartTitle = "Sales by year" chart.ChartTitleArea.IsBold = True chart.ChartTitleArea.Size = 12 Dim cs As Charts.ChartSerie = chart.Series(0) cs.CategoryLabels = sheet.Range("A2:A5") cs.Values = sheet.Range("B2:B5") cs.DataFormat.ShowActiveValue = True End Sub Private Sub CreateChartData(ByVal sheet As Worksheet) 'Country sheet.Range("A1").Value = "Year" sheet.Range("A2").Value = "2002" sheet.Range("A3").Value = "2003" sheet.Range("A4").Value = "2004" sheet.Range("A5").Value = "2005" 'Jun sheet.Range("B1").Value = "Sales" sheet.Range("B2").NumberValue = 4000 sheet.Range("B3").NumberValue = 6000 sheet.Range("B4").NumberValue = 7000 sheet.Range("B5").NumberValue = 8500 'Style sheet.Range("A1:B1").Style.Font.IsBold = True sheet.Range("A2:B2").Style.KnownColor = ExcelColors.LightYellow sheet.Range("A3:B3").Style.KnownColor = ExcelColors.LightGreen1 sheet.Range("A4:B4").Style.KnownColor = ExcelColors.LightOrange sheet.Range("A5:B5").Style.KnownColor = ExcelColors.LightTurquoise 'Border sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeTop).Color = Color.FromArgb(0, 0, 128) sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeBottom).Color = Color.FromArgb(0, 0, 128) sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeLeft).Color = Color.FromArgb(0, 0, 128) sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeRight).Color = Color.FromArgb(0, 0, 128) sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin sheet.Range("B2:C5").Style.NumberFormat = """$""#,##0" End Sub Private Sub ExcelDocViewer(ByVal fileName As String) Try System.Diagnostics.Process.Start(fileName) Catch End Try End Sub