C# OWC生成图表
程序员文章站
2024-03-09 12:26:35
1、如何安装owc组件 owc是office web compents的缩写,即microsoft的office web组件,包含spreadsheet组件、chart组件...
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饼图", "网状统计图", "弧线统计图" };
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饼图", "网状统计图", "弧线统计图" };
上一篇: java在文件尾部追加内容的简单实例