Aspose.Words使用教程之從零在word裡建立OOXML圖表

風靈使發表於2018-08-15

Index.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Word圖表.WordDemo" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnSimple" runat="server" Text="一個簡單的列圖表插入到文件" OnClick="btnSimple_Click" />
            <h2>有四種不同的載入新增方法:</h2>
            <asp:Button ID="btnColumn" runat="server" Text="插入列圖表" OnClick="btnColumn_Click" />
            <asp:Button ID="btnScatter" runat="server" Text="插入散點圖" OnClick="btnScatter_Click" />
            <asp:Button ID="btnArea" runat="server" Text="插入面積圖" OnClick="btnArea_Click" />
            <asp:Button ID="btnBubble" runat="server" Text="插入氣泡式圖表" OnClick="btnBubble_Click" />
        </div>
    </form>
</body>
</html>

Index.aspx.cs

using System;
using System.Web.UI;
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Drawing.Charts;

namespace Word圖表
{
    public partial class WordDemo : Page
    {

        public static string MyDir = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            MyDir = MapPath("~/File/");
        }

        protected void btnSimple_Click(object sender, EventArgs e)
        {
            var doc = new Document();
            var builder = new DocumentBuilder(doc);

            //新增包含預設資料的圖表。您可以指定不同的圖表型別和大小。
            var shape = builder.InsertChart(ChartType.Column, 432, 252);

            //Shape的Chart屬性包含所有與圖表相關的選項。
            var chart = shape.Chart;

            //chart.Title = "cs";

            //獲取圖表系列集合
            var seriesColl = chart.Series;

            //刪除預設生成的系列。
            seriesColl.Clear();

            //Create category names array, in this example we have two categories.
            //建立類別名稱陣列,在此示例中我們有兩個類別。
            string[] categories = { "第一賽季", "第二賽季", "第三賽季" };

            //新增新系列。資料陣列不能為空,並且陣列的大小必須相同。
            seriesColl.Add("AW Series 1", categories, new double[] { 1, 2, 2 });
            seriesColl.Add("AW Series 2", categories, new double[] { 3, 4, 44 });
            seriesColl.Add("AW Series 3", categories, new double[] { 5, 6, 22 });
            seriesColl.Add("AW Series 4", categories, new double[] { 7, 81, 3 });
            seriesColl.Add("AW Series 5", categories, new double[] { 9, 10, 44 });


            var wordPath = MapPath("/a.docx");
            doc.Save(wordPath);
        }

        protected void btnColumn_Click(object sender, EventArgs e)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            //條形圖
            Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
            Chart chart = shape.Chart;

            //使用此過載可將系列新增到任何型別的條形圖,柱形圖,折線圖和曲面圖。
            chart.Series.Add("AW Series 1", new string[] { "AW Category 1", "AW Category 2" }, new double[] { 1, 2 });


            doc.Save(MyDir + @"TestInsertColumnChart.docx");
        }

        protected void btnScatter_Click(object sender, EventArgs e)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            Shape shape = builder.InsertChart(ChartType.Scatter, 432, 252);
            Chart chart = shape.Chart;

            //使用此過載可將系列新增到任何型別的散點圖
            chart.Series.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 });

            doc.Save(MyDir + @"TestInsertScatterChart.docx");
        }

        protected void btnArea_Click(object sender, EventArgs e)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            Shape shape = builder.InsertChart(ChartType.Area, 432, 252);
            Chart chart = shape.Chart;

            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 });

            doc.Save(MyDir + @"TestInsertAreaChart.docx");
        }

        protected void btnBubble_Click(object sender, EventArgs e)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            Shape shape = builder.InsertChart(ChartType.Bubble, 432, 252);
            Chart chart = shape.Chart;

            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 });

            doc.Save(MyDir + @"TestInsertBubbleChart.docx");
        }


    }
}

執行結果:

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

相關文章