星空外围权威网站

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

原创|行业资讯|编辑:吉炜炜|2025-01-14 10:03:47.650|阅读 14 次

概述:Aspose.Words for .NET 允许您以编程方式生成和嵌入图表。本文将介绍如何使用 C# 在 Word 文档中创建图表。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

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、使用以下命令通过包管理器安装它:

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 文档中创建柱形图

使用 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 文档中创建散点图

使用 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 文档中插入面积图

使用 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 文档中插入气泡图

使用 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 文档中插入折线图

结论

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

————————————————————————————————————————————

欢迎下载|体验更多Aspose产品

点此获取更多Aspose产品信息 或 加入Aspose技术交流2群(666790229

aspose下载


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@atlvshi.cn

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
Aspose.Words for .NET

无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。

Aspose.Words for Reporting Services

Aspose.Words for Reporting Services 是独特的能在MS SQL Server Reporting Services中将RDL和RDLC报表导出为OOXML、DOC、RTF和WordprocessingML文档的解决方案。

Aspose.Words for JasperReports

Aspose.Words for JasperReports 可以帮助客户将报表从JasperReports 和 JasperServer 中导出为Microsoft Word document (DOC),Office Open XML (OOXML, DOCX),Rich Text Format (RTF),OpenDocument Text (ODT),Web page (HTML) 和纯text (TXT) 格式。

Aspose.Words for SharePoint

Aspose.Words for SharePoint是一个独特的解决方案,它支持文档转换,使在Microsoft SharePoint应用程序内转换和合并文档成为可能,并高度准确的支持多文档格式。

Aspose.Words for Android via Java

从Android应用程序中生成,操作,转换和渲染Word文件,而不依赖于Microsoft Word。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP