OWC组件生成柱状图

1、OWC介绍
在Microsoft Office 2003中包含有一组称为OWC的新控件集合。利用这些组件,可以在Web浏览器及其他传统的编程环境下,创建许多有用的数据分析解决方案和报表生成解决方案。下面几篇文章是使用OWC组件生成图表方面的相关知识。

OWC是与Microsoft Office一起安装的一组ActiveX控件。如果在计算机上安装了Office Web组件,则可以在Internet Explorer 5.01 SP2或更高版本中对电子表格、数据透视表和图表进行交互访问。如果在Microsoft Access的数据访问页中使用这些组件,则需要安装Internet Explorer 5.01 SP2或更高版本。如果计算机上未安装Office Web组件,可以从Microsoft公司的网站下载Office Web组件。这样,用户无须在计算机上安装Office软件,就可使用OWC。

OWC库中包含了Spreadsheet(电子数据表)组件、Chart(图表)组件、PivotTable(数据透视表)组件和Data Source(数据源)4个组件。Office Web Components的非凡之处在于它们可以在诸如Web页面、Visual Basic表单等控件容器中使用,也可在内存中作为不可见的对象使用。大多数COM控件只能在控件容器中作为可视控件使用,而大多数不可见对象则只能在内存中使用,而不能放入表单中或Web页面中。

2、设计思路

在使用OWC组件生成图片之前,首先需要将OWC组件引用到网站工程中。引用OWC组件之后,需要创建一个图表控件,然后在图表控件中添加一个图表对象,并且设置图表对象的相关属性,获得图表块的数据信息,最后使用图表控件的方法生成一个保存有图表数据信息的图片,在Web网页中通过调用该图片显示生成的图表信息,具体实现的设计思路如下图所示。



3、前台代码
<img alt="" src="temp.gif" style="width: 600px; height: 450px" />

4、后台代码
4.1、项目添加引用 Microsoft Office Web Components 11.0。
4.2、在文件里面引用如下

复制代码

using Microsoft.Office.Interop.Owc11;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

复制代码

4.3、具体程序代码

复制代码

namespace GenerateCharts
{
    public partial class Column : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //创建图表空间
            ChartSpace myspace = new ChartSpace();
            //添加一个图表对象
            ChChart mychart = myspace.Charts.Add(0);
            mychart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形
            //mychart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
            //mychart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
            //mychart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
            //mychart.Type = ChartChartTypeEnum.chChartTypeArea3D;//3D面积图
            //mychart.Type = ChartChartTypeEnum.chChartTypeBar3D;//3D条形图
            //mychart.Type = ChartChartTypeEnum.chChartTypeColumn3D;//3D柱形图
            //...等


            //设置图表相关属性
            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 = "人数";

            //连接并且打开数据库
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();
            string strsqls = "select 籍贯, count(籍贯) as 人数 from tb_ygxx group by 籍贯";
            SqlDataAdapter adsa = new SqlDataAdapter(strsqls, sqlcon);
            DataSet adds = new DataSet();
            adsa.Fill(adds);

            if (adds.Tables[0].Rows.Count > 0)
            {
                //添加图表
                for (int i = 0; i < adds.Tables[0].Rows.Count; i++)
                {
                    mychart.SeriesCollection.Add(0);
                }
                //添加图表数据
                for (int j = 0; j < adds.Tables[0].Rows.Count; j++)
                {
                    //设置图表块属性
                    mychart.SeriesCollection[j].Caption = adds.Tables[0].Rows[j][0].ToString();
                    mychart.SeriesCollection[j].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, adds.Tables[0].Rows[j][0].ToString());
                    mychart.SeriesCollection[j].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Convert.ToInt32(adds.Tables[0].Rows[j][1].ToString()));
                }
            }
            sqlcon.Close();
            //生成图表
            myspace.ExportPicture(Server.MapPath(".") + @"\temp.gif", "gif", 600, 450);
        }
    }
}

复制代码

注:在自己项目Web.config里面配置数据库连接串。

4.4、数据结果如下



5、效果图如下

参考:http://book.51cto.com/art/201112/308970.htm

Retrieving the COM class factory for component with CLSID {0002E55D-0000-0000-C000-000000000046} failed due to the following error: 80040154. 收藏 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0002E55D-0000-0000-C000-000000000046} failed due to the following error: 80040154. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0002E55D-0000-0000-C000-000000000046} failed due to the following error: 80040154.] GraphPage.Page_Load(Object sender, EventArgs e) +1097 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082 请大家帮忙 谢谢 Server Error in '/' Application. -------------------------------------------------------------------------------- Retrieving the COM class factory for component with CLSID {0002E55D-0000-0000-C000-000000000046} failed due to the following error: 80040154.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值