【ChatGPT】Spire.Doc 添加Chart图表到Word文档

Spire.Doc是一款.NET库,用于创建和修改Microsoft Word文档。要在Word文档中添加图表,可以使用Spire.Doc提供的Chart类。以下是一个简单的示例,展示了如何使用Spire.Doc创建一个图表并将其添加到Word文档中:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using Spire.Doc.Fields.Shapes.Charts;

// 创建一个新的Word文档
Document document = new Document();

// 添加一个段落
Paragraph paragraph = document.AddSection().AddParagraph();

// 创建一个图表
ShapeObject shape = paragraph.AppendChart(ChartType.Bar3D, 500, 300);
Chart chart = shape.Chart;

// 添加图表数据
chart.Series.Add("E-iceblue Test Series 1",
      new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
      new double[] { 1900000, 850000, 2100000, 600000, 1500000 });

chart.Series.Add("E-iceblue Test Series 2",
     new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
     new double[] { 900000, 50000, 1100000, 400000, 2500000 });

chart.Series.Add("E-iceblue Test Series 3",
      new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
      new double[] { 500000, 820000, 1500000, 400000, 100000 });

// 设置图表样式
ChartTitle title = chart.Title;
title.Text = "My Chart";

// 保存文档
document.SaveToFile("output.docx", FileFormat.Docx);

在这个示例中,我们创建了一个新的Word文档,并添加了一个段落。然后,我们创建了一个3D柱形图,并添加了一些数据。我们还设置了图表的样式,例如标题、字体和大小。最后,我们将图表插入到段落中,并将文档保存为output.docx。你可以根据需要修改代码来满足自己的需求,例如可以调整图表的类型、大小和样式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值