如何在 C&num 中创建和编辑 Excel 图表;

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼特·宾

在 Excel 中,图表是数据的图形表示。它是一种可视化工具,用于以更易于理解和更有意义的方式显示和分析数据。Excel 提供多种图表类型,如条形图、折线图、饼图等,每种类型都适合不同类型的数据和分析。

IronXL 支持柱状图、散点图、折线图、饼图、条形图和区域图,并可配置系列名称、图例位置、图表标题和图表位置。


适用于Excel的C# NuGet库

安装使用 NuGet

Install-Package IronXL.Excel
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于Excel的C# NuGet库

安装使用 NuGet

Install-Package IronXL.Excel
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronXLNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变Excel。

适用于Excel的C# NuGet库 nuget.org/packages/IronXL.Excel/
Install-Package IronXL.Excel

考虑安装 IronXL DLL 直接。下载并手动安装到您的项目或GAC表单中: IronXL.zip

手动安装到你的项目中

下载DLL

创建图表示例

IronXL 支持柱状图、散点图、线图、饼图、条形图和面积图。要创建图表,我们必须分别指定一些事项。

1.首先使用 CreateChart 方法指定图表类型和在工作表中的位置。

2.使用 AddSeries 方法添加序列。该方法也接受单列数据,因为这对于某些图表类型来说已经足够。第一个参数代表横轴的值。第二个参数代表纵轴的值。

3.可选择指定系列名称、图表名称和图例位置。

4.调用 Plot 方法绘制图表。该方法将使用添加的所有数据绘制图表。多次调用该方法将绘制多个图表,而不是修改现有图表。

让我们根据 chart.xlsx Excel 文件。数据预览如下:

数据

柱状图

: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")
VB   C#
柱状图

折线图

由于折线图与柱状图一样可以表示很多信息,因此在两者之间切换非常简单。只需更改图表类型即可。

:path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-line-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("Line Chart");

// Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom);

// Plot the chart
chart.Plot();

workBook.SaveAs("lineChart.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("Line Chart")

' Set the legend position
chart.SetLegendPosition(LegendPosition.Bottom)

' Plot the chart
chart.Plot()

workBook.SaveAs("lineChart.xlsx")
VB   C#
折线图

饼图

饼图只需要一列数据。

: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")
VB   C#
饼图

编辑图表示例

您可以编辑现有图表中的一些内容。您可以编辑图例位置和图表标题。要编辑图表,必须首先通过访问图表属性检索图表,然后选择要编辑的目标图表。然后访问图表属性进行编辑。

: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")
VB   C#
之前
之后

移除图表示例

要从电子表格中删除现有图表,首先要从工作表对象的 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")
VB   C#

查克尼特·宾

软件工程师

Chaknith 是开发者中的福尔摩斯。他第一次意识到自己可能在软件工程方面有前途,是在他出于乐趣做代码挑战的时候。他的重点是 IronXL 和 IronBarcode,但他为能帮助客户解决每一款产品的问题而感到自豪。Chaknith 利用他从直接与客户交谈中获得的知识,帮助进一步改进产品。他的轶事反馈不仅仅局限于 Jira 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。