C# OWC生成圖表
更新時(shí)間:2009年06月11日 15:17:20 作者:
最近做一個(gè)項(xiàng)目,按客戶需求,需要生成一些報(bào)表,OWC是比較合適的組件.
1、如何安裝OWC組件
OWC是Office Web Compents的縮寫,即Microsoft的Office Web組件,包含SpreadSheet組件、Chart組件、PioTable組件和Data Source組件。
只要裝了 Office 辦公軟件 ,在 C:\Program Files\MSECache\owc11_12 中會(huì)有一個(gè)安裝文件: OWC11.msi (offic 2003)
2、安裝完成后,新建一個(gè)工程,再添加引用...-->com--> Microsoft Office Web components 11.0 在bin文件夾中:Interop.OWC10.dll
引用空間:using Microsoft.Office.Interop.Owc11;
private void MakeLineChart()
{
//Y坐標(biāo)軸
string[] DataName = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
//第一條曲線的數(shù)據(jù)
int[] Data = { 0, 10, 20, 100, 40, 50, 60 };
//第二條曲線的數(shù)據(jù)
int[] Data1 = { 100, 50, 41, 86 };
//第三條曲線的數(shù)據(jù)
int[] Data2 = { 10, 50, 100, 30, 50, 60 };
string strValue1 = "";
string strValue = "";
string strValue2 = "";
string strCateory = "";
//循環(huán)取得數(shù)據(jù)并格式化為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();//創(chuàng)建ChartSpace對象來放置圖表
OWC10.ChChart myChart = mySpace.Charts.Add(0);//在ChartSpace對象中添加圖表,Add方法返回chart對象
myChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnClustered;//指定圖表的類型為線性圖
myChart.HasLegend = true;//指定圖表是否需要圖例
myChart.HasTitle = true;//給定標(biāo)題
myChart.Title.Caption = "交易曲線圖"; //圖表名稱
//給定X\Y軸的圖示說明
myChart.Axes[0].HasTitle = true;
myChart.Axes[0].Title.Caption = "數(shù)量"; //橫軸名稱
myChart.Axes[1].HasTitle = true;
myChart.Axes[1].Title.Caption = "日期"; //縱軸名稱
//添加一個(gè)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);
//添加一個(gè)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);
//添加一個(gè)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); //輸出圖表
//創(chuàng)建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[] { "垂直柱狀統(tǒng)計(jì)圖", "3D垂直柱狀統(tǒng)計(jì)圖", "水平柱狀統(tǒng)計(jì)圖", "3D水平柱狀統(tǒng)計(jì)圖", "區(qū)域統(tǒng)計(jì)圖", "3D區(qū)域統(tǒng)計(jì)圖", "中空餅圖", "折線統(tǒng)計(jì)圖", "3D折線統(tǒng)計(jì)圖", "折線帶點(diǎn)統(tǒng)計(jì)圖", "餅圖", "3D餅圖", "網(wǎng)狀統(tǒng)計(jì)圖", "弧線統(tǒng)計(jì)圖" };
OWC是Office Web Compents的縮寫,即Microsoft的Office Web組件,包含SpreadSheet組件、Chart組件、PioTable組件和Data Source組件。
只要裝了 Office 辦公軟件 ,在 C:\Program Files\MSECache\owc11_12 中會(huì)有一個(gè)安裝文件: OWC11.msi (offic 2003)
2、安裝完成后,新建一個(gè)工程,再添加引用...-->com--> Microsoft Office Web components 11.0 在bin文件夾中:Interop.OWC10.dll
引用空間:using Microsoft.Office.Interop.Owc11;
復(fù)制代碼 代碼如下:
private void MakeLineChart()
{
//Y坐標(biāo)軸
string[] DataName = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
//第一條曲線的數(shù)據(jù)
int[] Data = { 0, 10, 20, 100, 40, 50, 60 };
//第二條曲線的數(shù)據(jù)
int[] Data1 = { 100, 50, 41, 86 };
//第三條曲線的數(shù)據(jù)
int[] Data2 = { 10, 50, 100, 30, 50, 60 };
string strValue1 = "";
string strValue = "";
string strValue2 = "";
string strCateory = "";
//循環(huán)取得數(shù)據(jù)并格式化為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();//創(chuàng)建ChartSpace對象來放置圖表
OWC10.ChChart myChart = mySpace.Charts.Add(0);//在ChartSpace對象中添加圖表,Add方法返回chart對象
myChart.Type = OWC10.ChartChartTypeEnum.chChartTypeColumnClustered;//指定圖表的類型為線性圖
myChart.HasLegend = true;//指定圖表是否需要圖例
myChart.HasTitle = true;//給定標(biāo)題
myChart.Title.Caption = "交易曲線圖"; //圖表名稱
//給定X\Y軸的圖示說明
myChart.Axes[0].HasTitle = true;
myChart.Axes[0].Title.Caption = "數(shù)量"; //橫軸名稱
myChart.Axes[1].HasTitle = true;
myChart.Axes[1].Title.Caption = "日期"; //縱軸名稱
//添加一個(gè)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);
//添加一個(gè)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);
//添加一個(gè)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); //輸出圖表
//創(chuàng)建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[] { "垂直柱狀統(tǒng)計(jì)圖", "3D垂直柱狀統(tǒng)計(jì)圖", "水平柱狀統(tǒng)計(jì)圖", "3D水平柱狀統(tǒng)計(jì)圖", "區(qū)域統(tǒng)計(jì)圖", "3D區(qū)域統(tǒng)計(jì)圖", "中空餅圖", "折線統(tǒng)計(jì)圖", "3D折線統(tǒng)計(jì)圖", "折線帶點(diǎn)統(tǒng)計(jì)圖", "餅圖", "3D餅圖", "網(wǎng)狀統(tǒng)計(jì)圖", "弧線統(tǒng)計(jì)圖" };
相關(guān)文章
文本框中輸入小寫字母即時(shí)轉(zhuǎn)換為大寫實(shí)現(xiàn)思路
系統(tǒng)中有一個(gè)文本框,要求輸入大寫字母,只是用戶不是那么配合所以只好在程序來控制了,感興趣的朋友可以參考下哈2013-03-03
ASP.NET GridView中加入RadioButton不能單選的解決方案
這篇文章主要介紹了ASP.NET GridView中加入RadioButton不能單選的解決方案,希望大家閱讀完本文有所收獲。2015-09-09
理解ASP.NET Core 依賴注入(Dependency Injection)
把有依賴關(guān)系的類放到容器中,解析出這些類的實(shí)例,就是依賴注入。目的是實(shí)現(xiàn)類的解耦。本文主要介紹了ASP.NET Core 依賴注入(Dependency Injection),需要了解具體內(nèi)容的可以仔細(xì)閱讀這篇文章,希望對你有所幫助2021-09-09
Asp.Net服務(wù)器發(fā)送HTTP標(biāo)頭后無法設(shè)置內(nèi)容類型的問題解決
這篇文章主要給大家介紹了Asp.Net服務(wù)器發(fā)送HTTP標(biāo)頭后無法設(shè)置內(nèi)容類型問題的解決方法,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05
解決.Net Core項(xiàng)目發(fā)布在IIS上訪問404的問題
這篇文章介紹了解決.Net Core項(xiàng)目發(fā)布在IIS上訪問404的問題,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
AspNetPager分頁控件源代碼(Version 4.2)
AspNetPager分頁控件源代碼(Version 4.2)...2007-04-04
.NET Core使用HttpClient進(jìn)行表單提交時(shí)遇到的問題
這篇文章主要介紹了.NET Core使用HttpClient進(jìn)行表單提交時(shí)遇到的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

