如何在 C# 中创建和编辑 Excel 图表
IronXL.Excel 使 C# 开发人员能够使用简单的 API 调用,以编程方式创建、编辑和删除 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 创建 PDF 文件:
使用 NuGet 包管理器安装 IronXL
复制并运行这段代码。
// 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();部署到您的生产环境中进行测试
最小工作流程(5 个步骤)
- 下载用于创建和编辑图表的 C# 库。
- 准备创建图表的数据
- 使用
CreateChart方法设置图表类型和位置 - 使用
AddSeries方法添加数据系列 - 使用
Plot方法绘制图表
开始使用 IronXL
今天在您的项目中使用 IronXL,免费试用。
如何在 Excel 中创建图表? IronXL 支持柱状图、散点图、折线图、饼图、条形图和面积图。 要创建图表,请指定以下组件。 This flexibility allows you to create Excel spreadsheets with rich visualizations tailored to your data presentation needs. 1.使用 `CreateChart` 指定图表类型和工作表位置。 2.使用 `AddSeries` 添加系列。 对于某些图表类型,该方法只接受一列。第一个参数是横轴值。 二是纵轴值。 3.可选择指定系列名称、图表名称和图例位置。 4.调用 `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. 下面的示例演示了使用动物种群数据创建多序列柱状图: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-column-chart.cs ```
如何创建折线图? 折线图擅长显示随时间变化的趋势。由于折线图显示的信息与柱状图相同,因此在两者之间切换只需更改图表类型。 这使得折线图在[读取包含时间序列数据的 XLSX 文件](/csharp/excel/how-to/c-sharp-read-xlsx-file/)时特别有用: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs ```
何时应该使用饼图? 饼图显示整体的比例和百分比。 对于饼图,只需要一列数据,因此实施起来比较简单。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs ```
如何编辑现有图表? 在使用现有 Excel 文件时,您可能需要修改已创建的图表。 IronXL 提供了编辑现有图表的直接方法,允许您更新标题、重新定位图例和刷新数据。 This is useful when editing Excel files that contain pre-existing visualizations. 您可以编辑现有图表中的图例位置和图表标题。 要编辑图表,首先要通过访问 **Charts** 属性并选择目标图表来检索该图表。 然后访问图表属性进行编辑: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs ```
前

后
如何从 Excel 中删除图表? 有时您需要清理 Excel 文件,删除过时或不必要的图表。 This is common when managing worksheets containing multiple visualizations. 要从电子表格中删除现有图表,首先要从 **Charts** 属性中检索图表。 您将收到一份图表清单。 将目标图表对象传递给 `RemoveChart`: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs ``` ## 高级图表定制 除了基本的图表创建,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.Excel 为在 C# 应用程序中使用 Excel 图表提供了完整的解决方案。 无论是创建新的可视化图表、修改现有图表还是删除过时的图表,该库都能提供无需 Excel Interop 的直观方法。通过将图表功能与 IronXL 的其他功能(如数据操作和格式化)相结合,您可以构建复杂的 Excel 自动化解决方案,从而增强.NET 应用程序中的数据展示和分析功能。


如何创建折线图? 折线图擅长显示随时间变化的趋势。由于折线图显示的信息与柱状图相同,因此在两者之间切换只需更改图表类型。 这使得折线图在[读取包含时间序列数据的 XLSX 文件](/csharp/excel/how-to/c-sharp-read-xlsx-file/)时特别有用: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs ```
何时应该使用饼图? 饼图显示整体的比例和百分比。 对于饼图,只需要一列数据,因此实施起来比较简单。 They're effective when you want to convert spreadsheet data into visual representations of market share, budget allocation, or category distribution: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-pie-chart.cs ```
如何编辑现有图表? 在使用现有 Excel 文件时,您可能需要修改已创建的图表。 IronXL 提供了编辑现有图表的直接方法,允许您更新标题、重新定位图例和刷新数据。 This is useful when editing Excel files that contain pre-existing visualizations. 您可以编辑现有图表中的图例位置和图表标题。 要编辑图表,首先要通过访问 **Charts** 属性并选择目标图表来检索该图表。 然后访问图表属性进行编辑: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs ```
前

后
如何从 Excel 中删除图表? 有时您需要清理 Excel 文件,删除过时或不必要的图表。 This is common when managing worksheets containing multiple visualizations. 要从电子表格中删除现有图表,首先要从 **Charts** 属性中检索图表。 您将收到一份图表清单。 将目标图表对象传递给 `RemoveChart`: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs ``` ## 高级图表定制 除了基本的图表创建,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.Excel 为在 C# 应用程序中使用 Excel 图表提供了完整的解决方案。 无论是创建新的可视化图表、修改现有图表还是删除过时的图表,该库都能提供无需 Excel Interop 的直观方法。通过将图表功能与 IronXL 的其他功能(如数据操作和格式化)相结合,您可以构建复杂的 Excel 自动化解决方案,从而增强.NET 应用程序中的数据展示和分析功能。


如何编辑现有图表? 在使用现有 Excel 文件时,您可能需要修改已创建的图表。 IronXL 提供了编辑现有图表的直接方法,允许您更新标题、重新定位图例和刷新数据。 This is useful when editing Excel files that contain pre-existing visualizations. 您可以编辑现有图表中的图例位置和图表标题。 要编辑图表,首先要通过访问 **Charts** 属性并选择目标图表来检索该图表。 然后访问图表属性进行编辑: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-edit-chart.cs ```
前

后
如何从 Excel 中删除图表? 有时您需要清理 Excel 文件,删除过时或不必要的图表。 This is common when managing worksheets containing multiple visualizations. 要从电子表格中删除现有图表,首先要从 **Charts** 属性中检索图表。 您将收到一份图表清单。 将目标图表对象传递给 `RemoveChart`: ```csharp :path=/static-assets/excel/content-code-examples/how-to/create-edit-charts-remove-chart.cs ``` ## 高级图表定制 除了基本的图表创建,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.Excel 为在 C# 应用程序中使用 Excel 图表提供了完整的解决方案。 无论是创建新的可视化图表、修改现有图表还是删除过时的图表,该库都能提供无需 Excel Interop 的直观方法。通过将图表功能与 IronXL 的其他功能(如数据操作和格式化)相结合,您可以构建复杂的 Excel 自动化解决方案,从而增强.NET 应用程序中的数据展示和分析功能。

前

后
常见问题解答
如何在不使用 Interop 的情况下用 C# 编程创建 Excel 图表?
IronXL.Excel 提供了一个简单的 API,可在 C# 中创建 Excel 图表,而无需依赖 Interop。您可以使用 CreateChart 方法指定图表类型和位置,使用 AddSeries 方法添加数据,使用 Plot 方法渲染图表--所有这些都可以通过本地 C# 代码实现。
使用 C# 可以在 Excel 电子表格中创建哪些类型的图表?
IronXL 支持创建各种图表类型,包括柱状图、散点图、线图、饼图、条形图和面积图。您可以在调用 CreateChart 方法时指定图表类型,并使用标题、图例和数据序列自定义每个图表。
如何以编程方式在 Excel 图表中添加数据序列?
使用 IronXL 的 AddSeries 方法为图表添加数据。该方法接受单元格范围作为参数--第一个参数用于水平轴值,第二个参数用于垂直轴值。您可以添加多个序列来创建多序列图表。
使用 C# 在 Excel 中创建折线图的最快方法是什么?
使用 IronXL,您只需几行代码就能创建一个折线图:使用 CreateChart(ChartType.Line) 来初始化图表,使用 AddSeries() 来添加数据范围,使用 SetTitle() 来创建图表标题,使用 Plot() 来在工作表上渲染图表。
能否自定义标题和图例位置等图表属性?
是的,IronXL.Excel 允许完全自定义 Excel 图表。您可以使用 SetTitle() 添加图表标题,使用 SetLegendPosition() 放置图例(顶部、底部、左侧、右侧),还可以选择指定系列名称,以便更好地识别数据。
我需要安装 Microsoft Excel 才能以编程方式创建图表吗?
不,IronXL.Excel 可独立运行,无需安装 Microsoft Excel。它可以在内部处理所有 Excel 文件操作和图表创建,因此非常适合无法安装 Excel 的服务器环境和应用程序。






