The trendline TEXT value cannot be read from one of the two charts in the same sheet.

jasonILHOPARK 1 Reputation point
2021-10-21T09:10:08.637+00:00

142328-image1.png

142260-image2.png

Excel.Workbook workBook = appXLS.Workbooks.Open(fullPathWaveLength);
Excel.Worksheet workSheet = workBook.Worksheets1;
Excel.ChartObjects chartObjects = workSheet.ChartObjects();
Excel.ChartObject chartObject1 = workSheet.ChartObjects(1);
Excel.ChartObject chartObject2 = workSheet.ChartObjects(2);
GetTrendLine(chartObject1);
GetTrendLine(chartObject2);

void GetTrendLine(Excel.ChartObject chartObject)
{
Excel.Chart chart = chartObject.Chart;
Excel.Series series = (Excel.Series)chart.SeriesCollection(1);
Excel.Trendline trendline = series.Trendlines(1);
Excel.DataLabel dataLabel = trendline.DataLabel;
Console.WriteLine(dataLabel.Text);
}

These are very simple code.
But I don't know what make the error.

142279-image3.png

I ordered both charts to be read at the same time, but as you can see, there was only one result.
I want to get even a little hint.

C#
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.
10,229 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,479 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,639 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-10-27T06:11:42.973+00:00

    @jasonILHOPARK , based on my test, I find that your code is almost correct. I used my excel file and related code to get all trendline text value.

    Code:

      static void Main(string[] args)  
            {  
                Excel.Application appXLS = new Excel.Application();  
                Excel.Workbook workBook = appXLS.Workbooks.Open("D:\\test1.xlsx");  
                Excel.Worksheet workSheet = workBook.Worksheets[1];  
                Excel.ChartObjects chartObjects = workSheet.ChartObjects();  
                Excel.ChartObject chartObject;  
                Excel.Chart chart;  
                Excel.Trendline trendline;  
                Excel.DataLabel dataLabel;  
                Excel.Series series;  
                for (int i = 1; i <= chartObjects.Count; i++)  
                {  
                        chartObject = workSheet.ChartObjects(i);  
                        chart = chartObject.Chart;  
                        series = (Excel.Series)chart.SeriesCollection(1);  
                        trendline = series.Trendlines(1);  
                        dataLabel = trendline.DataLabel;  
                        Console.WriteLine(dataLabel.Text);  
      
                }  
                workBook.Close();  
                 
      
                Console.ReadKey();  
                  
      
            }  
    

    Excel file:

    144029-image.png

    Result:

    143940-image.png


    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.