Charu

Charu

  • NA
  • 128
  • 7.8k

How to use Excel setsourcedata in C#?

Oct 6 2016 5:37 AM
I'm struggling with one problem in my project from one week , please someone help me for this , I have written code in C# for connecting excel and updating the data's , Now its all working fine , and the last part is the newly updated data should appear in my chart graph which is there in the same excel file "Chart 1" . i'm failing to this , please anybody suggest me .please check my code below . i think i'm failing at setsourcedata , and when i run it in macro i found this.
 
 
// Macro result 
 ActiveSheet.ChartObjects("Chart 1").Activate 
ActiveChart.SetSourceData Source:=Range("Trd_NM!$A$2:$AC$7")
 ActiveChart.SetSourceData Source:=Range("Trd_NM!$A$2:$AD$7")
<
//C# code 
Excel.Workbook xlWorkBook;
 Excel._Worksheet sheet;
 Excel.ChartObject chartObj;
 Excel._Chart chart; 
Excel.Series series; 
object useDefault = Type.Missing; 
Excel.Workbook book;   
   xlWorkBook = app.Workbooks.Open(@"E:\\te\\DT");//Open the work book                
 sheet = xlWorkBook.Sheets["Trd_NM"];//Select the sheet the chart is on   
  chart = sheet.ChartObjects("Chart 1").chart;        
         chart.PlotArea.Select();             
    chart.SetSourceData(sheet.Range["A2:A6""], Excel.XlRowCol.xlColumns);
>


Answers (2)