1、如何安裝OWC組件 OWC是Office Web Compents的縮寫,即Microsoft的Office Web組件,包含SpreadSheet組件、Chart組件、PioTable組件和Data Source組件。
只要裝了 Office 辦公軟件 ,在 C:\Program Files\MSECache\owc11_12 中會有一個安裝文件: OWC11.msi (offic 2003)
2、安裝完成後,新建一個工程,再添加引用...-->com--> Microsoft Office Web components 11.0 在bin文件夾中:Interop.OWC10.dll
引用空間:using Microsoft.Office.Interop.Owc11;
復制代碼 代碼如下:
private void MakeLineChart()
{
//Y坐標軸
string[] DataName = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
//第一條曲線的數據
int[] Data = { 0, 10, 20, 100, 40, 50, 60 };
//第二條曲線的數據
int[] Data1 = { 100, 50, 41, 86 };
//第三條曲線的數據
int[] Data2 = { 10, 50, 100, 30, 50, 60 };
string strValue1 = "";
string strValue = "";
string strValue2 = "";
string strCateory = "";
//循環取得數據並格式化為OWC10需要的格式,(加'\t')
for (int i = 0; i < DataName.Length; i++)
{
strCateory += DataName[i] + '\t';
}
for (int i = 0; i < Data.Length; i++)
{
strValue += Data[i].ToString() + '\t';
}
for (int i = 0; i < Data1.Length; i++)
{
strValue1 += Data1[i].ToString() + '\t';
}
for (int i = 0; i < Data2.Length; i++)
{
strValue2 += Data2[i].ToString() + '\t';
}
OWC10.ChartSpaceClass mySpace = new OWC10.ChartSpaceClass();//創建ChartSpace對象來放置圖表
OWC10.ChChart myChart = mySpace.Charts.Add(0);//在ChartSpace對象中添加圖表,Add方法返回chart對象
myChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnClustered;//指定圖表的類型為線性圖
myChart.HasLegend = true;//指定圖表是否需要圖例
myChart.HasTitle = true;//給定標題
myChart.Title.Caption = "交易曲線圖"; //圖表名稱
//給定X\Y軸的圖示說明
myChart.Axes[0].HasTitle = true;
myChart.Axes[0].Title.Caption = "數量"; //橫軸名稱
myChart.Axes[1].HasTitle = true;
myChart.Axes[1].Title.Caption = "日期"; //縱軸名稱
//添加一個series(序列)
myChart.SeriesCollection.Add(0);
//給定series的名字
myChart.SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, "購買");
//給定series的分類
myChart.SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimCategories, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strCateory);
//給定具體值
myChart.SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimValues, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strValue);
//添加一個series
myChart.SeriesCollection.Add(1);
//給定series的名字
myChart.SeriesCollection[1].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, "出售");
//給定series的分類
myChart.SeriesCollection[1].SetData(OWC10.ChartDimensionsEnum.chDimCategories, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strCateory);
//給定具體值
myChart.SeriesCollection[1].SetData(OWC10.ChartDimensionsEnum.chDimValues, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strValue1);
//添加一個series
myChart.SeriesCollection.Add(2);
//給定series的名字
myChart.SeriesCollection[2].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, "總成交");
//給定series的分類
myChart.SeriesCollection[2].SetData(OWC10.ChartDimensionsEnum.chDimCategories, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strCateory);
//給定具體值
myChart.SeriesCollection[2].SetData(OWC10.ChartDimensionsEnum.chDimValues, (int)OWC10.ChartSpecialDataSourcesEnum.chDataLiteral, strValue2);
//輸出成GIF文件
string strAbsolutePath = (Server.MapPath(".")) + @"\Images\tempChart.gif";
Response.Write(strAbsolutePath);
mySpace.ExportPicture(strAbsolutePath, "GIF", 300, 300); //輸出圖表
//創建GIF文件的相對路徑
string strRelativePath = "Images/tempChart.gif";
//把圖片添加到Image
Image1.ImageUrl = strRelativePath;
}
//圖表類型枚舉
OWC11.ChartChartTypeEnum[] chartTypes = new OWC11.ChartChartTypeEnum[]{
ChartChartTypeEnum.chChartTypeColumnClustered,
ChartChartTypeEnum.chChartTypeColumn3D,
ChartChartTypeEnum.chChartTypeBarClustered,
ChartChartTypeEnum.chChartTypeBar3D,
ChartChartTypeEnum.chChartTypeArea,
ChartChartTypeEnum.chChartTypeArea3D,
ChartChartTypeEnum.chChartTypeDoughnut,
ChartChartTypeEnum.chChartTypeLineStacked,
ChartChartTypeEnum.chChartTypeLine3D,
ChartChartTypeEnum.chChartTypeLineMarkers,
ChartChartTypeEnum.chChartTypePie,
ChartChartTypeEnum.chChartTypePie3D,
ChartChartTypeEnum.chChartTypeRadarSmoothLine,
ChartChartTypeEnum.chChartTypeSmoothLine};
string[] chartTypesCh = new string[] { "垂直柱狀統計圖", "3D垂直柱狀統計圖", "水平柱狀統計圖", "3D水平柱狀統計圖", "區域統計圖", "3D區域統計圖", "中空餅圖", "折線統計圖", "3D折線統計圖", "折線帶點統計圖", "餅圖", "3D餅圖", "網狀統計圖", "弧線統計圖" };