question

jasonILHOPARK-2696 avatar image
0 Votes"
jasonILHOPARK-2696 asked jasonILHOPARK-2696 commented

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

142328-image1.png


142260-image2.png


Excel.Workbook workBook = appXLS.Workbooks.Open(fullPathWaveLength);
Excel.Worksheet workSheet = workBook.Worksheets[1];
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.

dotnet-csharpoffice-excel-itprooffice-scripts-excel-dev
image1.png (34.6 KiB)
image2.png (34.4 KiB)
image3.png (10.0 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@jasonILHOPARK-2696, what text value do you want to get? Such as 580,2032.3480, theses values? I am not good at Excel designing. I can not make data labels for Excel Trendline. Could you provide an excel file with me so that I can make a test?

0 Votes 0 ·

@JackJJun-MSFT

Thank you for your help.
This is the excel file link.

http://naver.me/5eGCsrjL

And the value What I want to get were marked with a red square box in the image below.

142887-image4.png


0 Votes 0 ·
image4.png (492.4 KiB)

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered jasonILHOPARK-2696 commented

@jasonILHOPARK-2696 , 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.



image.png (52.9 KiB)
image.png (1.7 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JackJJun-MSFT

Thank you for your answer.

I think that the code run normally too.

But, looking at the image file I asked in the first question, I was able to get only one equation out of the two charts.
I would like to know for some reason why I can't read the equation from one of the two charts in the same excel file.

And did you test the file I sent by link with your code?
I think there is the reason in the Excel I sent.

0 Votes 0 ·

@jasonILHOPARK-2696, Yes, I tested your excel file. I found that I can only get two equations of your excel. I think what causes this to happen is that you're using the same charts, causing them all to have the same equation.

0 Votes 0 ·

@JackJJun-MSFT

Thank you for your answer.

I did not use same chart to get the two equations.
I got one equation from one chart.

0 Votes 0 ·
Show more comments