Excel charts

Deepak Banavathu 26 Reputation points
2021-11-08T10:33:29.793+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.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Jack J Jun 25,316 Reputation points
    2021-11-09T02:58:20.48+00:00

    @Deepak Banavathu , Welcome to Microsoft Q&A,

    You could refer to the following code to create a Combo Chart by using microsoft.office.interop dll.

    Code:(using Excel = Microsoft.Office.Interop.Excel;)

        Excel.Application app = new Excel.Application();  
        Excel.Workbook workbook = app.Workbooks.Open("D:\\1.xlsx");  
        Excel.Worksheet worksheet = workbook.Sheets[1];  
        var charts = worksheet.ChartObjects() as Excel.ChartObjects;  
        var chartObject = charts.Add(60, 10, 300, 300) as Excel.ChartObject;  
        var chart = chartObject.Chart;  
        var range = worksheet.get_Range("A1", "D5");  
        chart.SetSourceData(range);  
        chart.FullSeriesCollection(1).ChartType = Excel.XlChartType.xlColumnClustered;  
        chart.FullSeriesCollection(2).ChartType = Excel.XlChartType.xlColumnClustered;  
        chart.FullSeriesCollection(3).ChartType = Excel.XlChartType.xlLine;  
        chart.FullSeriesCollection(3).AxisGroup = Excel.XlAxisGroup.xlSecondary;  
      
        chart.ChartWizard(Source: range,  
            Title: "Graph Title",  
            CategoryTitle: "X value",  
            ValueTitle: "Y value");  
        //Set chart range.  
       // Save.  
       workbook.Save();  
       workbook.Close();  
    

    Result:

    147547-image.png

    Hope the code example could help you.

    Best Regards,
    Jack


    If the answer is the right solution, 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.


1 additional answer

Sort by: Most helpful
  1. David 151 Reputation points
    2022-01-14T06:55:46.927+00:00

    As an alternative solution, you can use Spire.XLS for .NET to create a chart that combines clustered column chart and line chart, and export it as an image.

    Here are useful links that you can refer to.

    C#/VB.NET - Create a Combination Chart in Excel
    C#/VB.NET - Save Excel Charts as Images

    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.