Aspose.Words for .NET下载地址 https://soft51.cc/software/175811283999782847
图表和SmartArt是现代文档中展示数据和概念的重要工具。Aspose.Words提供了强大的图表创建和编辑功能,支持多种图表类型和SmartArt图形。本教程将介绍如何使用Aspose.Words for .NET创建专业的图表和SmartArt,提升文档的数据可视化效果。
Aspose.Words支持多种图表类型:
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Drawing.Charts;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Size = 16;
builder.Font.Bold = true;
builder.Writeln("销售数据分析报告");
builder.Font.Bold = false;
builder.Writeln();
// 1. 创建柱状图
builder.Writeln("季度销售对比:");
Shape columnChart = builder.InsertChart(ChartType.Column, 400, 300);
Chart chart1 = columnChart.Chart;
// 清除默认数据
chart1.Series.Clear();
// 添加数据系列
ChartSeries series1 = chart1.Series.Add("2023年",
new string[] { "Q1", "Q2", "Q3", "Q4" },
new double[] { 1200000, 1400000, 1600000, 1800000 });
ChartSeries series2 = chart1.Series.Add("2024年",
new string[] { "Q1", "Q2", "Q3", "Q4" },
new double[] { 1350000, 1550000, 1750000, 2000000 });
// 设置图表标题
chart1.Title.Text = "季度销售额对比(万元)";
chart1.Title.Show = true;
builder.Writeln();
builder.Writeln();
// 2. 创建折线图
builder.Writeln("月度增长趋势:");
Shape lineChart = builder.InsertChart(ChartType.Line, 400, 250);
Chart chart2 = lineChart.Chart;
chart2.Series.Clear();
chart2.Series.Add("销售增长率",
new string[] { "1月", "2月", "3月", "4月", "5月", "6月" },
new double[] { 8.5, 12.3, 15.7, 18.2, 22.1, 25.4 });
chart2.Title.Text = "月度销售增长率(%)";
chart2.Title.Show = true;
builder.Writeln();
builder.Writeln();
// 3. 创建饼图
builder.Writeln("产品销售占比:");
Shape pieChart = builder.InsertChart(ChartType.Pie, 350, 350);
Chart chart3 = pieChart.Chart;
chart3.Series.Clear();
chart3.Series.Add("产品占比",
new string[] { "产品A", "产品B", "产品C", "产品D", "其他" },
new double[] { 35.5, 28.3, 18.7, 12.2, 5.3 });
chart3.Title.Text = "产品销售占比分析";
chart3.Title.Show = true;
doc.Save("基础图表创建.docx");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("动态数据图表示例");
builder.Writeln();
// 创建复杂的柱状图
Shape chartShape = builder.InsertChart(ChartType.Column, 500, 350);
Chart chart = chartShape.Chart;
// 清除默认系列
chart.Series.Clear();
// 模拟从数据库或其他源获取的数据
var salesData = new[]
{
new { Region = "华北", Q1 = 850, Q2 = 920, Q3 = 1050, Q4 = 1180 },
new { Region = "华东", Q1 = 1200, Q2 = 1350, Q3 = 1480, Q4 = 1620 },
new { Region = "华南", Q1 = 680, Q2 = 750, Q3 = 820, Q4 = 950 },
new { Region = "西部", Q1 = 450, Q2 = 520, Q3 = 580, Q4 = 640 }
};
string[] categories = { "Q1", "Q2", "Q3", "Q4" };
Color[] seriesColors = { Color.Blue, Color.Green, Color.Orange, Color.Red };
// 为每个地区添加数据系列
for (int i = 0; i < salesData.Length; i++)
{
var data = salesData[i];
double[] values = { data.Q1, data.Q2, data.Q3, data.Q4 };
ChartSeries series = chart.Series.Add(data.Region, categories, values);
// 设置系列颜色
if (i < seriesColors.Length)
{
for (int j = 0; j < series.DataPoints.Count; j++)
{
series.DataPoints[j].Format.Fill.ForeColor = seriesColors[i];
}
}
}
// 图表整体格式设置
chart.Title.Text = "各地区季度销售业绩(万元)";
chart.Title.Show = true;
// 设置坐标轴
ChartAxis xAxis = chart.AxisX;
xAxis.Title.Text = "季度";
xAxis.Title.Show = true;
ChartAxis yAxis = chart.AxisY;
yAxis.Title.Text = "销售额(万元)";
yAxis.Title.Show = true;
yAxis.MajorUnit = 200;
// 设置图例
chart.Legend.Show = true;
chart.Legend.Position = LegendPosition.Bottom;
// 设置数据标签
foreach (ChartSeries series in chart.Series)
{
series.DataLabels.ShowValue = true;
series.DataLabels.NumberFormat.FormatCode = "#,##0";
}
builder.Writeln();
builder.Writeln("上图展示了各地区的季度销售对比,可以清晰看出华东地区表现最佳。");
doc.Save("动态数据图表.docx");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// 创建专业样式的组合图表
builder.Font.Size = 18;
builder.Font.Bold = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("企业财务分析图表");
builder.Font.Bold = false;
builder.Font.Size = 12;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Writeln();
// 收入趋势图
Shape revenueChart = builder.InsertChart(ChartType.Line, 450, 250);
Chart chart1 = revenueChart.Chart;
chart1.Series.Clear();
ChartSeries revenueSeries = chart1.Series.Add("月收入",
new string[] { "1月", "2月", "3月", "4月", "5月", "6月" },
new double[] { 180, 220, 195, 280, 315, 350 });
// 美化折线图
revenueSeries.Format.Line.Color = Color.Blue;
revenueSeries.Format.Line.Weight = 3.0;
revenueSeries.Marker.Symbol = MarkerSymbol.Circle;
revenueSeries.Marker.Size = 8;
revenueSeries.Marker.Format.Fill.ForeColor = Color.Red;
// 设置数据标签
revenueSeries.DataLabels.ShowValue = true;
revenueSeries.DataLabels.Font.Size = 10;
revenueSeries.DataLabels.Font.Bold = true;
revenueSeries.DataLabels.NumberFormat.FormatCode = "¥#,##0万";
chart1.Title.Text = "月度收入趋势";
chart1.Title.Font.Size = 14;
chart1.Title.Font.Bold = true;
// 设置图表背景
chart1.PlotArea.Format.Fill.PresetTextured(PresetTexture.Parchment);
builder.Writeln();
// 支出结构饼图
Shape expenseChart = builder.InsertChart(ChartType.Doughnut, 300, 300);
Chart chart2 = expenseChart.Chart;
chart2.Series.Clear();
ChartSeries expenseSeries = chart2.Series.Add("支出结构",
new string[] { "人工成本", "营销费用", "研发投入", "管理费用", "其他" },
new double[] { 45, 25, 15, 10, 5 });
// 设置饼图颜色
Color[] pieColors = { Color.LightBlue, Color.LightGreen, Color.LightYellow, Color.LightCoral, Color.LightGray };
for (int i = 0; i < expenseSeries.DataPoints.Count; i++)
{
expenseSeries.DataPoints[i].Format.Fill.ForeColor = pieColors[i % pieColors.Length];
expenseSeries.DataPoints[i].Format.Stroke.ForeColor = Color.White;
expenseSeries.DataPoints[i].Format.Stroke.Weight = 2;
}
// 设置数据标签显示百分比
expenseSeries.DataLabels.ShowPercentage = true;
expenseSeries.DataLabels.ShowCategoryName = true;
expenseSeries.DataLabels.Separator = "\n";
expenseSeries.DataLabels.Font.Size = 10;
chart2.Title.Text = "支出结构分析";
chart2.Title.Font.Size = 14;
chart2.Title.Font.Bold = true;
builder.Writeln();
builder.Writeln("以上图表展示了企业的收入增长趋势和支出结构,为决策提供数据支持。");
doc.Save("专业图表样式.docx");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Size = 16;
builder.Font.Bold = true;
builder.Writeln("组织架构与流程展示");
builder.Font.Bold = false;
builder.Writeln();
// 注意:Aspose.Words对SmartArt的支持有限,主要是读取和基本操作
// 更复杂的SmartArt通常需要在Word中创建,然后通过Aspose.Words处理
builder.Writeln("项目管理流程:");
builder.Writeln();
// 使用形状模拟SmartArt效果
CreateProcessDiagram(builder);
builder.Writeln();
builder.Writeln();
builder.Writeln("团队层级结构:");
builder.Writeln();
CreateHierarchyDiagram(builder);
doc.Save("SmartArt示例.docx");
private static void CreateProcessDiagram(DocumentBuilder builder)
{
// 创建流程图样式的SmartArt效果
string[] processes = { "需求分析", "方案设计", "开发实施", "测试验证", "上线部署" };
Color[] processColors = { Color.LightBlue, Color.LightGreen, Color.LightYellow, Color.LightCoral, Color.LightPink };
for (int i = 0; i < processes.Length; i++)
{
// 创建圆角矩形
Shape processBox = builder.InsertShape(ShapeType.RoundRectangle, 100, 60);
processBox.Left = 50 + i * 110;
processBox.Top = 50;
processBox.FillColor = processColors[i];
processBox.Stroke.Color = Color.DarkGray;
processBox.Stroke.Weight = 1.5;
processBox.WrapType = WrapType.None;
// 添加文本
builder.MoveTo(processBox.FirstParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 10;
builder.Font.Bold = true;
builder.Write(processes[i]);
// 添加箭头(除了最后一个)
if (i < processes.Length - 1)
{
Shape arrow = new Shape(builder.Document, ShapeType.Arrow);
arrow.Left = 155 + i * 110;
arrow.Top = 75;
arrow.Width = 40;
arrow.Height = 10;
arrow.FillColor = Color.Gray;
arrow.Stroke.Color = Color.DarkGray;
arrow.WrapType = WrapType.None;
builder.CurrentParagraph.AppendChild(arrow);
}
}
builder.MoveToDocumentEnd();
}
private static void CreateHierarchyDiagram(DocumentBuilder builder)
{
// 创建层级结构图
// 顶层 - CEO
Shape ceoBox = CreateHierarchyBox(builder, "CEO", 150, 40, 250, 50, Color.Gold);
// 连接线
CreateVerticalLine(builder, 325, 90, 30);
CreateHorizontalLine(builder, 200, 120, 250);
// 中层管理
CreateHierarchyBox(builder, "技术总监", 80, 35, 100, 140, Color.LightBlue);
CreateHierarchyBox(builder, "销售总监", 80, 35, 220, 140, Color.LightGreen);
CreateHierarchyBox(builder, "财务总监", 80, 35, 340, 140, Color.LightCoral);
// 连接到中层的垂直线
CreateVerticalLine(builder, 140, 175, 20);
CreateVerticalLine(builder, 260, 175, 20);
CreateVerticalLine(builder, 380, 175, 20);
// 底层员工
CreateHierarchyBox(builder, "开发团队", 70, 30, 105, 200, Color.AliceBlue);
CreateHierarchyBox(builder, "销售团队", 70, 30, 225, 200, Color.LightCyan);
CreateHierarchyBox(builder, "财务团队", 70, 30, 345, 200, Color.MistyRose);
}
private static Shape CreateHierarchyBox(DocumentBuilder builder, string text, double width, double height,
double left, double top, Color fillColor)
{
Shape box = builder.InsertShape(ShapeType.Rectangle, width, height);
box.Left = left;
box.Top = top;
box.FillColor = fillColor;
box.Stroke.Color = Color.DarkBlue;
box.Stroke.Weight = 1.0;
box.WrapType = WrapType.None;
builder.MoveTo(box.FirstParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 9;
builder.Font.Bold = true;
builder.Write(text);
return box;
}
private static void CreateVerticalLine(DocumentBuilder builder, double x, double y, double height)
{
Shape line = new Shape(builder.Document, ShapeType.Line);
line.Left = x;
line.Top = y;
line.Width = 1;
line.Height = height;
line.Stroke.Color = Color.Black;
line.Stroke.Weight = 1.0;
line.WrapType = WrapType.None;
builder.CurrentParagraph.AppendChild(line);
}
private static void CreateHorizontalLine(DocumentBuilder builder, double x, double y, double width)
{
Shape line = new Shape(builder.Document, ShapeType.Line);
line.Left = x;
line.Top = y;
line.Width = width;
line.Height = 1;
line.Stroke.Color = Color.Black;
line.Stroke.Weight = 1.0;
line.WrapType = WrapType.None;
builder.CurrentParagraph.AppendChild(line);
}
public static class ChartHelper
{
// 创建标准柱状图
public static Shape CreateColumnChart(DocumentBuilder builder, string title,
string[] categories, Dictionary<string, double[]> seriesData, int width = 400, int height = 300)
{
Shape chartShape = builder.InsertChart(ChartType.Column, width, height);
Chart chart = chartShape.Chart;
chart.Series.Clear();
chart.Title.Text = title;
chart.Title.Show = true;
foreach (var kvp in seriesData)
{
chart.Series.Add(kvp.Key, categories, kvp.Value);
}
// 应用标准格式
chart.Legend.Position = LegendPosition.Bottom;
chart.AxisY.NumberFormat.FormatCode = "#,##0";
return chartShape;
}
// 创建标准折线图
public static Shape CreateLineChart(DocumentBuilder builder, string title,
string[] categories, Dictionary<string, double[]> seriesData, int width = 400, int height = 250)
{
Shape chartShape = builder.InsertChart(ChartType.Line, width, height);
Chart chart = chartShape.Chart;
chart.Series.Clear();
chart.Title.Text = title;
chart.Title.Show = true;
foreach (var kvp in seriesData)
{
ChartSeries series = chart.Series.Add(kvp.Key, categories, kvp.Value);
series.Format.Line.Weight = 2.0;
series.DataLabels.ShowValue = true;
}
return chartShape;
}
// 设置图表主题色彩
public static void ApplyColorTheme(Chart chart, Color[] colors)
{
int colorIndex = 0;
foreach (ChartSeries series in chart.Series)
{
Color seriesColor = colors[colorIndex % colors.Length];
foreach (ChartDataPoint point in series.DataPoints)
{
point.Format.Fill.ForeColor = seriesColor;
}
colorIndex++;
}
}
// 导出图表数据
public static string ExportChartData(Chart chart)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("图表数据导出");
sb.AppendLine($"标题: {chart.Title.Text}");
sb.AppendLine();
foreach (ChartSeries series in chart.Series)
{
sb.AppendLine($"系列: {series.Name}");
for (int i = 0; i < series.XValues.Count; i++)
{
sb.AppendLine($" {series.XValues[i]}: {series.YValues[i]}");
}
sb.AppendLine();
}
return sb.ToString();
}
}
public void CreateBusinessDashboard()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// 设置页面布局
builder.PageSetup.Orientation = Orientation.Landscape;
builder.PageSetup.LeftMargin = 36;
builder.PageSetup.RightMargin = 36;
// 标题
builder.Font.Size = 20;
builder.Font.Bold = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("企业经营仪表板");
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Font.Size = 12;
builder.Font.Bold = false;
builder.Writeln();
// KPI指标卡片
CreateKPICards(builder);
builder.Writeln();
builder.Writeln();
// 主要图表
string[] months = { "1月", "2月", "3月", "4月", "5月", "6月" };
// 收入趋势图
var revenueData = new Dictionary<string, double[]>
{
["当年收入"] = new double[] { 1200, 1350, 1180, 1480, 1620, 1750 },
["去年同期"] = new double[] { 1100, 1250, 1080, 1320, 1480, 1580 }
};
ChartHelper.CreateLineChart(builder, "月度收入对比(万元)", months, revenueData, 350, 200);
// 在同一行添加产品占比饼图
builder.Write("\t"); // 添加间距
Shape pieChart = builder.InsertChart(ChartType.Pie, 250, 200);
Chart pie = pieChart.Chart;
pie.Series.Clear();
pie.Series.Add("产品销售占比",
new string[] { "产品A", "产品B", "产品C", "产品D" },
new double[] { 40, 30, 20, 10 });
pie.Title.Text = "产品销售占比";
pie.Title.Show = true;
// 应用配色主题
Color[] themeColors = { Color.Blue, Color.Green, Color.Orange, Color.Red, Color.Purple };
ChartHelper.ApplyColorTheme(pie, themeColors);
builder.Writeln();
builder.Writeln();
// 地区销售对比
var regionData = new Dictionary<string, double[]>
{
["华北"] = new double[] { 350, 380, 420, 450 },
["华东"] = new double[] { 480, 520, 560, 600 },
["华南"] = new double[] { 280, 300, 340, 380 },
["西部"] = new double[] { 180, 200, 220, 250 }
};
ChartHelper.CreateColumnChart(builder, "地区季度销售对比(万元)",
new string[] { "Q1", "Q2", "Q3", "Q4" }, regionData, 500, 250);
// 添加数据说明
builder.Writeln();
builder.Font.Size = 10;
builder.Font.Italic = true;
builder.Writeln("* 数据更新时间:" + DateTime.Now.ToString("yyyy-MM-dd"));
builder.Writeln("* 华东地区表现持续领先,西部地区增长潜力较大");
doc.Save("商业仪表板.docx");
}
private static void CreateKPICards(DocumentBuilder builder)
{
var kpis = new[]
{
new { Label = "总收入", Value = "¥8,850万", Change = "+12.5%", Color = Color.LightGreen },
new { Label = "新客户", Value = "1,247个", Change = "+18.2%", Color = Color.LightBlue },
new { Label = "订单量", Value = "3,568单", Change = "+8.7%", Color = Color.LightYellow },
new { Label = "客户满意度", Value = "96.2%", Change = "+2.1%", Color = Color.LightCoral }
};
foreach (var kpi in kpis)
{
Shape card = builder.InsertShape(ShapeType.RoundRectangle, 140, 80);
card.FillColor = kpi.Color;
card.Stroke.Color = Color.Gray;
card.Stroke.Weight = 1.0;
card.WrapType = WrapType.None;
builder.MoveTo(card.FirstParagraph);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 10;
builder.Font.Bold = true;
builder.Write(kpi.Label);
builder.InsertBreak(BreakType.LineBreak);
builder.Font.Size = 16;
builder.Font.Color = Color.DarkBlue;
builder.Write(kpi.Value);
builder.InsertBreak(BreakType.LineBreak);
builder.Font.Size = 9;
builder.Font.Color = Color.Green;
builder.Write(kpi.Change);
builder.Font.Color = Color.Black;
builder.Write("\t"); // 添加间距
}
builder.MoveToDocumentEnd();
}
本教程介绍了Aspose.Words for .NET中图表和SmartArt处理的核心功能:
通过合理运用图表和可视化元素,可以让文档的数据展示更加直观、专业,大大提升信息传达的效果和说服力。
Aspose.Words for .NET下载地址 https://soft51.cc/software/175811283999782847