Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:33:51+00:00 2026-06-18T02:33:51+00:00

I’m trying to create a new LineChart on excel worksheet, and i don’t know

  • 0

I’m trying to create a new LineChart on excel worksheet, and i don’t know how can i feel data on this chart and how to add values for each series.
The following is what i try to do:

    static void Main(string[] args)
    {
        string docName = @"myFile.xlsx";
        string worksheetName = "Sheet1";
        string title = "My Chart";
        InsertChartInSpreadsheet(docName, worksheetName, title);
    }


    private static void InsertChartInSpreadsheet(string docName, string worksheetName, string title)
    {

    // Open Document for editing
        using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
        {
            IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().
    Where(s => s.Name == worksheetName);
            if (sheets.Count() == 0)
            {
                // The specified worksheet does not exist.
                return;
            }

            WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);

            // Add a new drawing to the worksheet.
            DrawingsPart drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
            worksheetPart.Worksheet.Append(new DocumentFormat.OpenXml.Spreadsheet.Drawing() { Id = worksheetPart.GetIdOfPart(drawingsPart) });
            worksheetPart.Worksheet.Save();

            // Add a new chart and set the chart language to English-US.
            ChartPart chartPart = drawingsPart.AddNewPart<ChartPart>();
            chartPart.ChartSpace = new ChartSpace();
            chartPart.ChartSpace.Append(new EditingLanguage() { Val = new StringValue("en-US") });
            DocumentFormat.OpenXml.Drawing.Charts.Chart chart = chartPart.ChartSpace.AppendChild<DocumentFormat.OpenXml.Drawing.Charts.Chart>(
                new DocumentFormat.OpenXml.Drawing.Charts.Chart());

            // Create a new clustered column chart.
            PlotArea plotArea = chart.AppendChild<PlotArea>(new PlotArea());
            Layout layout = plotArea.AppendChild<Layout>(new Layout());
            uint i = 0;
            LineChart lineChart = plotArea.AppendChild<LineChart>(new LineChart());
            LineChartSeries[] series = new LineChartSeries() {} ;
            uint j ; 
            // add series to the lineChart
            for (LineChartSeries s in series) {
                s = lineChart.AppendChild<LineChartSeries>(new LineChartSeries());
                s.SeriesText = new SeriesText(new NumericValue() { Text = "Ser "+j } );
                series1.Index = new Index() { Val = new UInt32Value(j) };
                series1.Order = new Order() { Val = new UInt32Value(j) } ;
                j++;
            }
            i = 1;

            /***
            Here is my question , how can i add values to my chart
            Example data: 
                    Ser1    Ser2    Ser3    Ser4
            2010    5       6       12      41
            2011    65      1       31      43
            2012    75      8       64      40
            2013    12      31      47      66
            ***/

            // Save the chart part.
            chartPart.ChartSpace.Save();
            /* Here i'll add the position of the chart on the worksheet */ 
            // Save the WorksheetDrawing object.
            drawingsPart.WorksheetDrawing.Save();
        }
    }

in the code above i gave an example for the data that i want to feel on my chart :

                    Ser1    Ser2    Ser3    Ser4
            2010    5       6       12      41
            2011    65      1       31      43
            2012    75      8       64      40
            2013    12      31      47      66

i’m beginner on C# and OpenXml , please help me 🙂
thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:33:52+00:00Added an answer on June 18, 2026 at 2:33 am

    After hours of searching, finally i found a solution for this problem by using the spreadsheetlight library , this library doesn’t the same thing as i want , but at the end i get the result which i hope.
    SpreadSheetLight, helps me to create a chart from a DataTable easily, then i used the following function to copy the chart from the excel file to new powerpoint file:

        public CopyChartFromXlsx2Pptx(string SourceFile, string TargetFile, string targetppt)
        {
    
            ChartPart chartPart;
    
            ChartPart newChartPart;
    
            //SlidePart slidepartbkMark = null;
    
            string chartPartIdBookMark = null;
    
            File.Copy(TargetFile, targetppt, true);
    
            //Powerpoint document
    
            using (OpenXmlPkg.PresentationDocument pptPackage = OpenXmlPkg.PresentationDocument.Open(targetppt, true))
            {
    
                OpenXmlPkg.PresentationPart presentationPart = pptPackage.PresentationPart;
    
                var secondSlidePart = pptPackage.PresentationPart.SlideParts.Skip(0).First();  // this will retrieve your second slide
    
                chartPart = secondSlidePart.ChartParts.First();
    
                chartPartIdBookMark = secondSlidePart.GetIdOfPart(chartPart);
    
                //Console.WriteLine("ID:"+chartPartIdBookMark.ToString());
    
                secondSlidePart.DeletePart(chartPart);
    
                secondSlidePart.Slide.Save();
    
                newChartPart = secondSlidePart.AddNewPart<ChartPart>(chartPartIdBookMark);
    
                ChartPart saveXlsChart = null;
    
                using (SpreadsheetDocument xlsDocument = SpreadsheetDocument.Open(SourceFile.ToString(), true))
                {
    
                    WorkbookPart xlsbookpart = xlsDocument.WorkbookPart;
    
                    foreach (var worksheetPart in xlsDocument.WorkbookPart.WorksheetParts)
                    {
    
                        if (worksheetPart.DrawingsPart != null)
    
                            if (worksheetPart.DrawingsPart.ChartParts.Any())
                            {
                                saveXlsChart = worksheetPart.DrawingsPart.ChartParts.First();
                            }
    
                    }
    
                    newChartPart.FeedData(saveXlsChart.GetStream());
                    //newChartPart.FeedData(
                    secondSlidePart.Slide.Save();
    
                    xlsDocument.Close();
    
                    pptPackage.Close();
    
                }
    
            }
        }
    

    After i can delete the generated excel file 🙂

    I hope this solution can help you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.