
在 Microsoft Word 中,图表使数据可视化变得简单而有效。它们将数字转化为视觉效果,帮助您的内容脱颖而出。您可以直接在 Word 中创建图表,以说明趋势、比较等。根据您的需求选择条形图、饼图、折线图和其他样式。Microsoft Word 具有内置的图表创建工具。然而,Aspose.Words for .NET 允许您以编程方式生成和嵌入图表。本文将解释 如何使用 C# 在 Word 文档中创建图表。
本文涵盖以下主题:
C# API 创建 Word 文档中的图表
我们将使用 Aspose.Words for .NET 在 Word 文档中创建不同类型的图表。这个强大的库允许您以编程方式创建、编辑和转换 Word 文件。其强大的 API 使图表的创建和自定义变得简单。开发人员可以无缝地将动态数据可视化集成到他们的文档工作流程中。这是生成报告或带有交互式图表的文档的绝佳工具。
要开始使用 Aspose.Words for .NET,请按照以下简单步骤操作:
在 Word 中创建柱状图
柱状图非常适合比较不同类别的数据。您可以使用 Aspose.Words for .NET 在 Word 文档中轻松创建柱状图。请按照以下步骤操作:
- 创建一个 Document 类实例。
- 使用 DocumentBuilder 类构建文档。
- 使用 InsertChart() 添加柱状图,传递 ChartType.Column、width 和 height 作为参数。
- 将结果存储在 Shape 对象中。
- 创建一个 Chart 类实例。
- 使用 Chart.Series 访问图表系列集合。
- 使用 Add() 方法添加数据并定义图表系列。
- 使用 Save() 方法保存文件。
下面的代码片段演示了 如何使用 C# 在 Word 文档中创建柱状图。
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add chart with default data. You can specify different chart types and sizes. | |
Shape shape = builder.InsertChart(ChartType.Column, 432, 252); | |
// Chart property of Shape contains all chart related options. | |
Chart chart = shape.Chart; | |
// Get chart series collection. | |
ChartSeriesCollection seriesColl = chart.Series; | |
// Check series count. | |
Console.WriteLine(seriesColl.Count); | |
// Delete default generated series. | |
seriesColl.Clear(); | |
// Create category names array, in this example we have two categories. | |
string[] categories = new string[] { "AW Category 1", "AW Category 2" }; | |
// Adding new series. Please note, data arrays must not be empty and arrays must be the same size. | |
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2 }); | |
seriesColl.Add("AW Series 2", categories, new double[] { 3, 4 }); | |
seriesColl.Add("AW Series 3", categories, new double[] { 5, 6 }); | |
seriesColl.Add("AW Series 4", categories, new double[] { 7, 8 }); | |
seriesColl.Add("AW Series 5", categories, new double[] { 9, 10 }); | |
// Save the document | |
doc.Save("column-chart.docx"); |

使用 C# 在 Word 文档中创建柱状图。
使用 C# 在 Word 文档中创建散点图
散点图对于显示两个变量之间的关系非常有用。要在 Word 文档中插入散点图,请按照之前的步骤操作。只需在 InsertChart() 方法中设置 ChartType.Scatter。
下面的代码示例展示了 如何使用 C# 在 Word 文档中创建散点图。
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Scatter chart. | |
Shape shape = builder.InsertChart(ChartType.Scatter, 432, 252); | |
Chart chart = shape.Chart; | |
// Use this overload to add series to any type of Scatter charts. | |
chart.Series.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 }); | |
// Save the document | |
doc.Save("scatter-chart.docx"); |

使用 C# 在 Word 文档中创建散点图。
使用 C# 在 Word 文档中插入区域图
区域图突出显示随时间变化的幅度。要在 Word 文档中创建区域图,请按照之前的步骤操作。只需在 InsertChart() 方法中设置 ChartType.Area。
下面的代码示例展示了 如何使用 C# 在 Word 文档中创建区域图。
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Area chart. | |
Shape shape = builder.InsertChart(ChartType.Area, 432, 252); | |
Chart chart = shape.Chart; | |
// Use this overload to add series to any type of Area, Radar and Stock charts. | |
chart.Series.Add("AW Series 1", new DateTime[] { | |
new DateTime(2002, 05, 01), | |
new DateTime(2002, 06, 01), | |
new DateTime(2002, 07, 01), | |
new DateTime(2002, 08, 01), | |
new DateTime(2002, 09, 01)}, | |
new double[] { 32, 32, 28, 12, 15 }); | |
// Save the document | |
doc.Save("area-chart.docx"); |

使用 C# 在 Word 文档中插入区域图。
使用 C# 在 Word 文档中插入气泡图
气泡图表非常适合展示数据的三个维度。按照之前的步骤在 Word 文档中创建气泡图表。只需在 InsertChart() 方法中设置 ChartType.Bubble。
下面的代码示例演示了 如何使用 C# 在 Word 文档中创建气泡图表。
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Bubble chart. | |
Shape shape = builder.InsertChart(ChartType.Bubble, 432, 252); | |
Chart chart = shape.Chart; | |
// Use this overload to add series to any type of Bubble charts. | |
chart.Series.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 }, new double[] { 10, 4, 8 }); | |
// Save the document | |
doc.Save("bubble-chart.docx"); |

在 Word 文档中插入气泡图表(使用 C#)
在 Word 文档中创建折线图(使用 C#)
折线图对于展示随时间变化的数据趋势非常有用。要在 Word 文档中创建折线图,请按照之前的步骤操作。只需在 InsertChart() 方法中设置 ChartType.Line。
下面的代码示例演示了 如何使用 C# 在 Word 文档中创建折线图。
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.InsertChart(ChartType.Line, 432, 252); | |
Chart chart = shape.Chart; | |
// Determines whether the title shall be shown for this chart. Default is true. | |
chart.Title.Show = true; | |
// Setting chart Title. | |
chart.Title.Text = "Sample Line Chart Title"; | |
// Determines whether other chart elements shall be allowed to overlap title. | |
chart.Title.Overlay = false; | |
// Please note if null or empty value is specified as title text, auto generated title will be shown. | |
// Determines how legend shall be shown for this chart. | |
chart.Legend.Position = LegendPosition.Left; | |
chart.Legend.Overlay = true; | |
// Save the document | |
doc.Save("line-chart.docx"); |

在 Word 文档中插入折线图(使用 C#)
获取免费许可证
解锁 Aspose.Words for .NET 的全部潜力!立即获取 免费临时许可证 并开始使用。享受无限制的文档操作。
Word 文档中的图表 – 有用的资源
除了在 Word 文档中处理图表,您还可以探索 Aspose.Words for .NET 的更多功能,以进一步自定义图表和文档。查看以下资源:
结论
本文介绍了如何使用 C# 在 Word 文档中创建不同类型的图表——柱形图、散点图、区域图和气泡图。我们演示了如何使用 Aspose.Words for .NET 来创建和自定义图表。按照这些步骤,您可以轻松地在 Word 文档中添加富有视觉效果的图表,从而增强数据分析和展示效果。
如果有任何疑问,请随时通过我们的 免费支持论坛 联系我们。