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);
var chart = shape.Chart;
var seriesColl = chart.Series;
seriesColl.Clear();
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");
}
}
}
執行結果: