Share via

Excel charts

Deepak Banavathu 26 Reputation points
2021-11-04T09:59:09.223+00:00

I am planning to create Combo Charts using c# can you please guide me how i can achieve that.

The chart is a combination of clustered column and line chart and i am planning to achieve it using microsoft.office.interop dll. to use it for reporting purpose.

Our existing application is using Macros and we want to upgrade it from macros to C# dll.

Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments

2 answers

Sort by: Most helpful
  1. David 151 Reputation points
    2021-12-21T03:04:07.263+00:00

    If you dot not mind using a 3rd party library, you could try Free Spire.Doc for .NET.

    The following is the code snippet for creating a combination chart in an Excel worksheet.

    using Spire.Xls;
    using Spire.Xls.Charts;
    using System.Drawing;
    
    namespace CreateCombinationChart
    {
        class Program
        {
            static void Main(string[] args)
            {
                //create a workbook
                Workbook workbook = new Workbook();          
                Worksheet sheet=workbook.Worksheets[0];
    
                //insert sample data with formatting into the sheet
                sheet.Range[“A1”].Value = “Month”;
                sheet.Range[“A2”].Value = “Jan”;
                sheet.Range[“A3”].Value = “Feb”;
                sheet.Range[“A4”].Value = “Mar”;
                sheet.Range[“B1”].Value = “Visits”;
                sheet.Range[“B2”].Value = “548”;
                sheet.Range[“B3”].Value = “256”;
                sheet.Range[“B4”].Value = “471”;
                sheet.Range[“C1”].Value = “Revenue”;
                sheet.Range[“C2”].Value = “530”;
                sheet.Range[“C3”].Value = “340”;
                sheet.Range[“C4”].Value = “673”;
                sheet.Range[“C2:C4”].NumberFormat = “$#,##0.00”;
                sheet.Range[“D1”].Value = “Conversion
    Rate”;
                sheet.Range[“D2”].NumberValue = 0.027;
                sheet.Range[“D3”].NumberValue = 0.034;
                sheet.Range[“D4”].NumberValue = 0.092;
                sheet.Range[“D2:D4”].NumberFormat = “0.00%”;
    
                //add a chart based the data from A1 to D4
                Chart chart = sheet.Charts.Add();
                chart.DataRange = sheet.Range[“A1:D4”];
                chart.ChartTitle = “Combination Chart”;
                chart.SeriesDataFromRange = false;
    
                //set position of chart
                chart.LeftColumn = 1;
                chart.TopRow = 7;
                chart.RightColumn = 9;
                chart.BottomRow = 25;
    
                //apply different chart type to different series
                var cs1 = (ChartSerie)chart.Series[0];
                cs1.SerieType = ExcelChartType.ColumnClustered;
                var cs2 = (ChartSerie)chart.Series[1];
                cs2.SerieType = ExcelChartType.ColumnClustered;
                var cs3 = (ChartSerie)chart.Series[2];
                cs3.SerieType = ExcelChartType.LineMarkers;
    
                //plot series 3 to Y-axis
    chart.SecondaryCategoryAxis.IsMaxCross = true;
                cs3.UsePrimaryAxis = false;
    
                //save the file
                workbook.SaveToFile(“result.xlsx”,ExcelVersion.Version2010);
            }
        }
    }
    

    Was this answer helpful?

    0 comments No comments

  2. Emily Hua-MSFT 27,901 Reputation points
    2021-11-05T01:24:47.47+00:00

    @Deepak Banavathu

    Welcome to Q&A forum~
    But tag "office-excel-itpro" focuses more on general issue of Excel client, the code issue is out of our support scope.
    Thanks for your understanding.

    To better help you, I would suggest post a new thread with the tag "dotnet-csharp" and "office-vba-dev" on Q&A forum.
    Besides, here is a similar thread "How to Create Combo chart using C# Interop Excel" that you could have a look.

    Hope the information could be helpful.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.