2016-09-26 2 views
0

을 사용하여 Excel에서 차트를 업데이트하는 중에 문제가 있습니다.Excel 차트의 데이터 범위가 프로그래밍 방식으로 C#

C# 코드를 사용하여 행 값을 삽입하지만 이제 데이터를 삽입 한 후 그래프를 완전히 표시하도록 차트 범위를 늘리고 싶습니다. 나는 아래의 코드를 시도했다.

어떻게 해결할 수 있는지 알려 주시기 바랍니다. 2 이미지에서

xlApp = new Excel.Application();   
xlWorkBook = xlApp.Workbooks.Open("E:\\test\\DT", 0, false, 5, "", "", false, 
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0); //@"H:\TestFile.xlsx" 
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets.get_Item(1); 
Workbook wb = xlApp.Workbooks.Add(XlSheetType.xlWorksheet); 
Worksheet ws = (Worksheet)xlApp.ActiveSheet; 
rng = ws.get_Range("A2", "A7"); 
// rng = ws.get_Range("Trend_NCM!$A$2:$AD$7",useDefault); 
// rng = ws.get_Range()     // //ActiveChart.SetSourceData 
Source = rng("Trend_NCM!$A$2:$AD$7"); 
Excel.ChartObject chartObject11 = (Excel.ChartObject)ws.ChartObjects("Trend NCM"); 
chartObject11.Activate(); 

, 난 난 그냥 단지마다 추가되는 새로운 컬럼에 대한 C#을하여 만들고 싶어, 수동으로 차트를 드래그하고 있습니다.

enter image description here

+0

Excel을 조작하기 위해 사용하는 인터페이스는 무엇입니까? 오피스. 인터럽트? C#으로 코드를 보여줄 수 있습니까? 더 구체적으로 말하십시오. – Taosique

+0

안녕하세요 저는 office.interop입니다. – CharanS

+0

안녕하세요 @ Taosique, C#을 사용하여 설정 소스 데이터 범위를 변경해야합니다. 피스가 어떤 아이디어를 갖고 있는지 알려주세요. – CharanS

답변

0
이 작품 같은

겠습니까 뭔가?

Excel.Application app = new Excel.Application(); 
Excel.Workbook book; 
Excel.Worksheet sheet; 
Excel.ChartObject chartObj; 
Excel.Chart chart; 
Excel.Series series; 

book = app.Workbooks.Open(@"Path to excel workbook");//Open the work book 
sheet = book.Sheets["Sheet name"];//Select the sheet the chart is on 
chartObj = sheet.ChartObjects("chart name");//Select the ChartObject 
chart = chartObj.Chart; //Select the Chart from the ChartObject 
series = chart.SeriesCollection(1);//Select the series you are changing 
series.Values = (Excel.Range)sheet.Range["A1:A5"]; //or sheet.Range[sheet.Cells[1, 1], sheet.Cells[4, 5]] works too 

편집 (또한 COM 개체의 최대 적절한 청소를 할 기억, 아래 필요한 값을 넣어) : 대신 단일 시리즈의 전체 데이터 소스를 업데이트 아래보십시오.

Excel.Application app = new Excel.Application(); 
Excel.Workbook book; 
Excel.Worksheet sheet; 
Excel.ChartObject chartObj; 
Excel.Chart chart; 

book = app.Workbooks.Open(@"Path to workbook");//Open the work book 
sheet = book.Sheets["sheet name"];//Select the sheet the chart is on 
chartObj = sheet.ChartObjects("Chart name");//Select the ChartObject 
chart = chartObj.Chart; //Select the Chart from the ChartObject 
chart.SetSourceData(sheet.Range["A1:A5"], Excel.XlRowCol.xlColumns); //I do not know whether your charts creates series by column or row, but I assumed column 
+0

안녕하세요, Kieran, 코드를 시도했지만 코드의 마지막 줄에'object '에' 'get_Range'에 대한 정의. 내 Excel 파일에서 차트 데이터 범위는 이제 다음과 같습니다 = Trend_NCM! $ A $ 2 : $ AB $ 7, 위의 사진에서 참조 할 수 있습니다. 단지 AB 열에서 AC로 차트를 증가시키고 싶습니다. 열. 너가 내 요점을 알았 으면 좋겠어. – CharanS

+0

Excel interop 도구의 V12.0.0.0 대신 V14.0.0.0에 대한 참조를 사용하고있을 수 있습니다. 업데이트 된 답변과 주석이 달린 코드를보십시오. –

+0

this를 사용하십시오. series.Values ​​= (Excel.Range) sheet.Range [sheet.Cells [1, 1], sheet.Cells [4, 5]]; – frankiehf

관련 문제