C#으로 xlsx 파일에서 Excel 차트 생성 및 편집하기
IronXL은 C# 개발자가 간단한 API 호출을 사용하여 xlsx 파일에서 프로그래밍적으로 Excel 차트를 만들고, 엑셀 편집을 통해 차트를 수정하거나 제거할 수 있도록 합니다. Excel Interop 종속성 없이 데이터에서 직접 컬럼, 라인, 파이 및 기타 차트 유형을 생성할 수 있습니다.
Excel에서 차트는 데이터를 시각적으로 표시하고 분석하는 데 사용되는 그래픽 표현입니다. Excel은 막대 차트, 라인 차트, 파이 차트 등과 같은 다양한 차트 유형을 제공하며, 각각은 다른 데이터 및 분석 요구에 적합합니다. When working with IronXL's comprehensive Excel library, you can programmatically create these visualizations to enhance your reports and dashboards.
빠른 시작: 몇 초 만에 라인 차트 생성 및 플롯
IronXL을 사용하면, 몇 줄만으로 설치하고, 워크북을 로드하고, CreateChart를 호출하고, 데이터 시리즈를 추가하고, 제목과 범례 위치를 설정하고, Plot를 할 수 있습니다. 이 예제는 네이티브 C# 메소드를 사용하여 Interop 오버헤드 없이 차트를 생성하는 방법을 보여줍니다.
-
NuGet 패키지 관리자를 사용하여 https://www.nuget.org/packages/IronXL.Excel 설치하기
PM > Install-Package IronXL.Excel -
다음 코드 조각을 복사하여 실행하세요.
// Load workbook and create a line chart with data series var chart = workSheet.CreateChart(ChartType.Line, 2, 2, 15, 10).AddSeries("A2:A10","B2:B10").Title = workSheet["B1"].StringValue; // Set title and legend position, then plot the chart chart.SetTitle("Quick Line Chart").SetLegendPosition(LegendPosition.Bottom).Plot(); -
실제 운영 환경에서 테스트할 수 있도록 배포하세요.
무료 체험판으로 오늘 프로젝트에서 IronXL 사용 시작하기
- 차트를 만들고 편집하기 위한 C# 라이브러리 다운로드
- 차트 작성을 위한 데이터를 준비하세요
- `CreateChart` 메소드를 사용하여 차트 유형 및 위치를 설정하세요
- `AddSeries` 메소드를 사용하여 데이터 시리즈를 추가하세요
- `Plot` 메소드를 사용하여 차트를 플롯하세요
IronXL로 시작해 보세요
Excel에서 차트를 어떻게 만드나요?
IronXL은 컬럼, 스캐터, 라인, 파이, 바, 영역 차트를 지원합니다. 차트를 만들려면 다음 구성 요소를 지정하세요. This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs.
CreateChart를 사용하여 차트 유형과 워크시트 위치를 지정합니다.AddSeries로 시리즈를 추가합니다. 이 메소드는 일부 차트 유형에 대해 단일 열을 허용합니다. 첫 번째 매개변수는 수평축 값입니다. 두 번째는 세로 축 값입니다.- 옵션으로 시리즈 이름, 차트 이름 및 범례 위치를 지정합니다.
Plot을(를) 호출하여 차트를 렌더링합니다. 여러 호출은 여러 차트를 생성합니다.
Let's create charts from the data in the chart.xlsx Excel file. A preview of the data is displayed below:
범례 차트를 만드는 프로세스는 무엇입니까?
범례 차트는 카테고리 간의 값을 비교하는 데 이상적입니다. When you load spreadsheet data, you can visualize it effectively using column charts to highlight differences between data points. 다음 예제는 동물 개체수 데이터로 멀티 시리즈 범례 차트를 생성하는 것을 보여줍니다:
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "C2:C7");
series.Title = workSheet["C1"].StringValue;
// Add the series
series = chart.AddSeries(xAxis, "D2:D7");
series.Title = workSheet["D1"].StringValue;
// Set the chart title
chart.SetTitle("Column Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("columnChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Column, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "C2:C7")
series.Title = workSheet("C1").StringValue
' Add the series
series = chart.AddSeries(xAxis, "D2:D7")
series.Title = workSheet("D1").StringValue
' Set the chart title
chart.SetTitle("Column Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("columnChart.xlsx")
선을 어떻게 만듭니까?
선 차트는 시간 경과에 따른 추세를 보여주기에 탁월합니다. 선 차트는 범례 차트와 동일한 정보를 표시하므로, 차트 유형을 변경하는 것만으로 전환이 가능합니다. 이는 XLSX 파일을 읽을 때 시간 시리즈 데이터를 포함할 때 특히 유용합니다:
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Set the chart title
chart.SetTitle("Pie Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Set the chart title
chart.SetTitle("Pie Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("pieChart.xlsx")
파이 차트는 언제 사용해야 합니까?
파이 차트는 전체의 비율과 백분율을 보여줍니다. 파이 차트의 경우 데이터 한 열만 필요하므로 구현이 간단합니다. They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution:
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("chart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Set the chart type and position
IChart chart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10);
string xAxis = "A2:A7";
// Add the series
IChartSeries series = chart.AddSeries(xAxis, "B2:B7");
series.Title = workSheet["B1"].StringValue;
// Set the chart title
chart.SetTitle("Pie Chart");
// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);
// Plot the chart
chart.Plot();
workBook.SaveAs("pieChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("chart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Set the chart type and position
Private chart As IChart = workSheet.CreateChart(ChartType.Pie, 5, 5, 20, 10)
Private xAxis As String = "A2:A7"
' Add the series
Private series As IChartSeries = chart.AddSeries(xAxis, "B2:B7")
series.Title = workSheet("B1").StringValue
' Set the chart title
chart.SetTitle("Pie Chart")
' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)
' Plot the chart
chart.Plot()
workBook.SaveAs("pieChart.xlsx")
기존 차트를 어떻게 편집합니까?
기존 Excel 파일을 작업할 때 이미 생성된 차트를 수정해야 할 수도 있습니다. IronXL은 기존 차트를 편집할 수 있는 간단한 방법을 제공하여 제목을 업데이트하고 범례를 재배치하고 데이터를 새로 고칠 수 있습니다. This is useful when editing Excel files that contain pre-existing visualizations.
기존 차트의 범례 위치와 차트 제목을 편집할 수 있습니다. 차트를 편집하려면 먼저 Charts 속성에 액세스하여 대상 차트를 선택하여 가져옵니다. 그런 다음 차트 속성에 접근하여 수정합니다:
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the chart
IChart chart = workSheet.Charts[0];
// Edit the legend position
chart.SetLegendPosition(LegendPosition.Top);
// Edit the chart title
chart.SetTitle("Edited Chart");
workBook.SaveAs("editedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the chart
Private chart As IChart = workSheet.Charts(0)
' Edit the legend position
chart.SetLegendPosition(LegendPosition.Top)
' Edit the chart title
chart.SetTitle("Edited Chart")
workBook.SaveAs("editedChart.xlsx")
이전
이후
Excel에서 차트를 어떻게 제거합니까?
때로는 오래되거나 불필요한 차트를 제거하여 Excel 파일을 정리해야 합니다. This is common when managing worksheets containing multiple visualizations. 스프레드시트에서 기존 차트를 제거하려면 먼저 Charts 속성에서 차트를 가져옵니다. 차트 목록을 받게 됩니다. 대상 차트 객체를 RemoveChart에 전달합니다:
:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs
using IronXL;
using IronXL.Drawing.Charts;
using System.Collections.Generic;
WorkBook workBook = WorkBook.Load("pieChart.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Retrieve the chart
List<IChart> chart = workSheet.Charts;
// Remove the chart
workSheet.RemoveChart(chart[0]);
workBook.SaveAs("removedChart.xlsx");
Imports IronXL
Imports IronXL.Drawing.Charts
Imports System.Collections.Generic
Private workBook As WorkBook = WorkBook.Load("pieChart.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Retrieve the chart
Private chart As List(Of IChart) = workSheet.Charts
' Remove the chart
workSheet.RemoveChart(chart(0))
workBook.SaveAs("removedChart.xlsx")
고급 차트 사용자 정의
기본 차트 생성 외에도 IronXL은 고급 사용자 정의 옵션을 지원합니다. When creating complex reports or dashboards, you can combine charts with other Excel features like conditional formatting to create comprehensive data visualizations.
비즈니스 애플리케이션에서는 종종 데이터베이스 쿼리나 실시간 데이터 소스에서 동적으로 생성되는 차트가 필요합니다. IronXL은 .NET 데이터 구조와 원활하게 통합되어 DataTables, Lists 또는 모든 열거 가능한 컬렉션에서 차트를 만들 수 있습니다. 이는 시각적 요소를 포함하는 자동화된 보고서를 생성하는 데 이상적입니다.
요약
IronXL은 C# 애플리케이션에서 Excel 차트를 작업할 수 있는 완전한 솔루션을 제공합니다. 새로운 시각화를 생성하든, 기존 시각화를 수정하든, 오래된 차트를 제거하든, Excel 인터롭을 필요로 하지 않는 직관적인 방법을 제공합니다. 차트 기능을 데이터 조작 및 서식과 같은 IronXL의 다른 기능과 결합하여, NET 애플리케이션에서 데이터 표현 및 분석을 개선하는 복잡한 Excel 자동화 솔루션을 구축할 수 있습니다.
자주 묻는 질문
Interop을 사용하지 않고 C#에서 Excel 차트를 프로그래밍 방식으로 생성하는 방법은 무엇인가요?
IronXL은 상호 운용성 종속성 없이 C#에서 Excel 차트를 생성할 수 있는 간단한 API를 제공합니다. CreateChart 메서드를 사용하여 차트 유형과 위치를 지정하고, AddSeries를 사용하여 데이터를 추가하고, Plot을 사용하여 차트를 렌더링할 수 있습니다. 이 모든 작업은 네이티브 C# 코드로 수행됩니다.
C#을 사용하여 Excel 스프레드시트에 어떤 유형의 차트를 만들 수 있나요?
IronXL은 막대형 차트, 산점도형 차트, 선형 차트, 원형 차트, 영역형 차트 등 다양한 차트 유형을 지원합니다. CreateChart 메서드를 호출할 때 차트 유형을 지정할 수 있으며, 제목, 범례, 데이터 계열을 사용하여 각 차트를 사용자 지정할 수 있습니다.
엑셀 차트에 데이터 계열을 프로그래밍 방식으로 추가하는 방법은 무엇인가요?
IronXL의 AddSeries 메서드를 사용하여 차트에 데이터를 추가할 수 있습니다. 이 메서드는 셀 범위를 매개변수로 받는데, 첫 번째 매개변수는 가로축 값, 두 번째 매개변수는 세로축 값입니다. 여러 개의 계열을 추가하여 다중 계열 차트를 만들 수 있습니다.
C#을 사용하여 Excel에서 선 그래프를 가장 빠르게 만드는 방법은 무엇입니까?
IronXL을 사용하면 단 몇 줄의 코드로 선 그래프를 만들 수 있습니다. CreateChart(ChartType.Line)을 사용하여 차트를 초기화하고, AddSeries()를 사용하여 데이터 범위를 추가하고, SetTitle()을 사용하여 차트 제목을 지정하고, Plot()을 사용하여 워크시트에 차트를 렌더링합니다.
차트 제목이나 범례 위치 같은 차트 속성을 사용자 지정할 수 있나요?
네, IronXL은 Excel 차트를 완벽하게 사용자 지정할 수 있도록 지원합니다. SetTitle() 함수를 사용하여 차트 제목을 추가하고, SetLegendPosition() 함수를 사용하여 범례의 위치(상단, 하단, 왼쪽, 오른쪽)를 지정할 수 있으며, 필요에 따라 데이터 식별을 위해 계열 이름을 지정할 수도 있습니다.
차트를 프로그램으로 생성하려면 Microsoft Excel이 설치되어 있어야 하나요?
아니요, IronXL은 Microsoft Excel 설치 없이 독립적으로 작동합니다. 모든 Excel 파일 작업 및 차트 생성을 자체적으로 처리하므로 Excel을 설치할 수 없는 서버 환경 및 애플리케이션에 적합합니다.

