原理:OWC是Office Web Compent的縮寫,即Microsoft的Office Web組件,它為在Web中繪製圖形提供了靈活的同時也是最基本的機制。在一個intranet環境中,如果可以假設客戶機上存在特定的瀏覽器和一些功能強大的軟體(如IE6和Office 2000/XP/2003),那麼就有能力利用Office
Web組件提供一個互動式圖形開發環境。這種模式下,用戶端工作站將在整個任務中分擔很大的比重。理論上說Excel能做的圖都可以通過OWC畫。
第一步:在網站中引用該組件。
第二步:點擊“添加引用”後彈出一個視窗,添加OWC的引用。:
第三步:代碼中引用Microsoft.Office.Interop.Owc11。
注意:如果使用的是vs2010編譯器,在引用成功之後owc11的引用屬性:在“引用”中右擊OWC11,點擊“屬性”
嵌入互操作類型:false
代碼:
前台:
<html
xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
後台:
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using Microsoft.Office.Interop.Owc11;
namespace Image
{
public partial class Image1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strSeriesName = "圖例1";
//存放資料
string[] MonNum = new string[12] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
string[] MonCount = new string[12] { "100", "200", "250", "150", "300", "400", "220", "350", "200", "180", "120", "280" };
//為X軸指定特定的字串以顯示資料
string strXdata = string.Empty;
foreach (string strData in MonNum)
{
strXdata += strData + "\t";
}
//為Y軸指定特定的字串
string strYData = string.Empty;
foreach (string strValue in MonCount)
{
strYData += strValue + "\t";
}
//建立ChartSpace來放置圖表
ChartSpace laySpace = new ChartSpaceClass();
//在ChartSpace對象中添加圖表
ChChart InsertChart = laySpace.Charts.Add(0);
//指定繪圖類型,可以通過ChartChartTypeEnum枚舉值取得
InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面積圖
//指定圖表是否需要圖例標註
InsertChart.HasLegend = false;
//為表徵圖添加標題
InsertChart.HasTitle = true;
InsertChart.Title.Caption = "流水賬";
//為X軸添加圖示說明
InsertChart.Axes[0].HasTitle = true;
InsertChart.Axes[0].Title.Caption = "";
//為Y軸添加圖示說明
InsertChart.Axes[1].HasTitle = true;
//InsertChart.Axes[1].Scaling.SplitMinimum = 100;
InsertChart.Axes[1].Title.Caption = "數量";
//添加一個Series系列
InsertChart.SeriesCollection.Add(0);
//給定Series系列的名字
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
//給定分類
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
//給定值
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYData);
//輸出檔案
string strAbsolutePath = Server.MapPath(".")+"\\ShowData.gif";
laySpace.ExportPicture(strAbsolutePath, "GIF", 400, 250);
string strRelativePath = "./ShowData.gif";
//把圖片添加在PlaceHolder1中,並在頁面上顯示
string strImageTag = "<IMG SRC='" + strRelativePath + "'>";
this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
}
}
}
效果: