利用.NET版Word处理控件Aspose.Words,使用 C# 在 Word 中创建图表

Microsoft Word 中的图表使数据可视化变得简单而有效。它们将数字转换为视觉效果,帮助您的内容脱颖而出。您可以直接在 Word 中创建图表来说明趋势、比较等。从条形图、饼图、折线图和其他样式中进行选择,以满足您的需求。Microsoft Word 具有用于创建图表的内置工具。但是,Aspose.Words for .NET 允许您以编程方式生成和嵌入图表。本博客介绍如何使用 C# 在 Word 文档中创建图表。

**Aspose.Words**是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

使用 C# API 在 Word 文档中创建图表

我们将使用Aspose.Words for .NET在 Word 文档中创建不同类型的图表。这个功能强大的库允许您以编程方式创建、编辑和转换 Word 文件。其强大的 API 使图表创建和自定义变得简单。开发人员可以将动态数据可视化无缝集成到他们的文档工作流程中。它是生成带有交互式图表的报告或文档的优选工具。

要开始使用 Aspose.Words for .NET,请按照以下简单步骤操作:

1、下载Aspose.Words for .NET

下载Aspose.words for.net

2、使用以下命令通过NuGet包管理器安装它:

复制代码
PM> Install-Package Aspose.Words

在 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");

使用 C# 在 Word 文档中插入气泡图

使用 C# 在 Word 文档中创建折线图

折线图可用于显示随时间变化的数据趋势。要在 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");

使用 C# 在 Word 文档中插入折线图

结论

在本文中,我们介绍了如何使用 C# 在 Word 文档中创建不同类型的图表(柱形图、散点图、面积图和气泡图)。我们演示了如何使用 Aspose.Words for .NET 创建和自定义图表。按照这些步骤,您可以轻松地将视觉上吸引人的图表添加到 Word 文档中,从而增强数据分析和演示。

相关推荐
一阵没来由的风1 小时前
拒绝造轮子(C#篇)ZLG CAN卡驱动封装应用
c#·can·封装·zlg·基础封装·轮子
zzzhpzhpzzz4 小时前
Win10快速安装.NET3.5
.net·win10
许泽宇的技术分享6 小时前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
一枚小小程序员哈7 小时前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
Eternity_GQM9 小时前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word
cimeo13 小时前
【C 学习】06-算法&程序设计举例
c#
百锦再14 小时前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
WYH2871 天前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
hqwest1 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农1 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net