原創(chuàng)|行業(yè)資訊|編輯:吉煒煒|2025-01-14 10:03:47.650|閱讀 142 次
概述:Aspose.Words for .NET 允許您以編程方式生成和嵌入圖表。本文將介紹如何使用 C# 在 Word 文檔中創(chuàng)建圖表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Microsoft Word 中的圖表使數(shù)據(jù)可視化變得簡單而有效。它們將數(shù)字轉(zhuǎn)換為視覺效果,幫助您的內(nèi)容脫穎而出。您可以直接在 Word 中創(chuàng)建圖表來說明趨勢、比較等。從條形圖、餅圖、折線圖和其他樣式中進行選擇,以滿足您的需求。Microsoft Word 具有用于創(chuàng)建圖表的內(nèi)置工具。但是,Aspose.Words for .NET 允許您以編程方式生成和嵌入圖表。本博客介紹如何使用 C# 在 Word 文檔中創(chuàng)建圖表。
Aspose.Words 是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
使用 C# API 在 Word 文檔中創(chuàng)建圖表
我們將使用Aspose.Words for .NET在 Word 文檔中創(chuàng)建不同類型的圖表。這個功能強大的庫允許您以編程方式創(chuàng)建、編輯和轉(zhuǎn)換 Word 文件。其強大的 API 使圖表創(chuàng)建和自定義變得簡單。開發(fā)人員可以將動態(tài)數(shù)據(jù)可視化無縫集成到他們的文檔工作流程中。它是生成帶有交互式圖表的報告或文檔的優(yōu)選工具。
要開始使用 Aspose.Words for .NET,請按照以下簡單步驟操作:
1、下載Aspose.Words for .NET
2、使用以下命令通過包管理器安裝它:
PM> Install-Package Aspose.Words
在 Word 中創(chuàng)建柱形圖
柱形圖非常適合比較不同類別的數(shù)據(jù)。您可以使用 Aspose.Words for .NET 在 Word 文檔中輕松創(chuàng)建柱形圖。請按以下步驟操作:
下面的代碼片段演示了如何使用 C# 在 Word 文檔中創(chuàng)建柱形圖。
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 文檔中創(chuàng)建柱形圖
使用 C# 在 Word 文檔中創(chuàng)建散點圖
散點圖對于顯示兩個變量之間的關(guān)系很有用。要在 Word 文檔中插入散點圖,請按照前面的步驟操作。只需在InsertChart()方法中設(shè)置ChartType.Scatter即可。
下面的代碼示例展示了如何使用 C# 在 Word 文檔中創(chuàng)建散點圖。
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 文檔中創(chuàng)建散點圖
使用 C# 在 Word 文檔中插入面積圖
面積圖突出顯示隨時間變化的幅度。要在 Word 文檔中創(chuàng)建面積圖,請按照上述步驟操作。只需在InsertChart()方法中設(shè)置ChartType.Area即可。
下面的代碼示例展示了如何使用 C# 在 Word 文檔中創(chuàng)建面積圖。
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 文檔中插入氣泡圖
氣泡圖非常適合顯示三維數(shù)據(jù)。按照前面的步驟在 Word 文檔中創(chuàng)建氣泡圖。只需在InsertChart()方法中設(shè)置ChartType.Bubble即可。
下面的代碼示例演示了如何使用 C# 在 Word 文檔中創(chuàng)建氣泡圖。
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 文檔中創(chuàng)建折線圖
折線圖可用于顯示隨時間變化的數(shù)據(jù)趨勢。要在 Word 文檔中創(chuàng)建折線圖,請按照上述步驟操作。只需在InsertChart()方法中設(shè)置ChartType.Line即可。
下面的代碼示例演示如何使用 C# 在 Word 文檔中創(chuàng)建折線圖。
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 文檔中插入折線圖
結(jié)論
在本文中,我們介紹了如何使用 C# 在 Word 文檔中創(chuàng)建不同類型的圖表(柱形圖、散點圖、面積圖和氣泡圖)。我們演示了如何使用 Aspose.Words for .NET 創(chuàng)建和自定義圖表。按照這些步驟,您可以輕松地將視覺上吸引人的圖表添加到 Word 文檔中,從而增強數(shù)據(jù)分析和演示。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)