利用.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 文档中,从而增强数据分析和演示。

相关推荐
曹牧3 小时前
C#:字符串做20位截断
开发语言·c#
为你奋斗!3 小时前
禅道Bug导出CSV文件批量转Word+图片离线部署操作手册
word·bug
MindUp4 小时前
Word 转 PPT 自动化实践:8 款 AI 生成工具的自然语言处理与排版效果横向评测
人工智能·word·powerpoint
界面开发小八哥5 小时前
界面控件DevExpress Blazor v26.1新版亮点 - TreeList & 数据编辑器
javascript·编辑器·.net·界面控件·blazor·devexpress·用户界面
离陌在学C#13 小时前
C# 事件(Event)详解:从概念到实战
开发语言·c#
rockey62715 小时前
C#脚本引擎之AScript与Flee对比
c#·.net·script·eval·动态脚本
zjnlswd21 小时前
C#学习笔记4
笔记·学习·c#
曹牧1 天前
C#:List<string>特定格式输出
windows·c#
reasonsummer1 天前
【办公类-109-12】20260901圆形挂牌(模版重制:文本框圆形重叠_word编辑单面_接送卡&被子卡&床卡&入园卡)
python·word
Z5998178411 天前
c#软件开发学习笔记--Modbus-TCP/UDP网口通讯
笔记·学习·c#