asp.net代码练习 work069 使用微软office办公软件附带的OWC绘制报表的示例

258 篇文章 2 订阅

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work069.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>使用微软office办公软件附带的OWC绘制报表的示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1">
            <tr>
                <td>柱状图</td>
                <td>饼图</td>
                <td>折线图</td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="imgChartTypeColumnClustered" runat="server" />
                </td>
                <td>
                    <asp:Image ID="imgPie" runat="server" />
                </td>
                <td>
                    <asp:Image ID="imgLine" runat="server" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Owc11;

namespace work069
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //首先去下载Microsoft.Office.Interop.Owc11.dll这个动态库,然后在项目中引用这个库,记得使用using包含进来。
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowChartTypeColumnClustered();
            ShowChartTypePie();
            ShowChartTypeLine();
        }

        /// <summary>
        /// 折线图
        /// </summary>
        private void ShowChartTypeLine()
        {
            //创建图形容器对象
            Microsoft.Office.Interop.Owc11.ChartSpace chartSpace1 = new ChartSpace();
            //添加图形对象
            Microsoft.Office.Interop.Owc11.ChChart chChart1 = chartSpace1.Charts.Add(0);
            //设定类型
            chChart1.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeLine;
            //添加系列对象           
            Microsoft.Office.Interop.Owc11.ChSeries chSeries1 = chChart1.SeriesCollection.Add(0);
            //设定系列的名字
            chSeries1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "1\t2\t3\t4");
            //设定分类
            chSeries1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12");
            //设定值
            chSeries1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "300\t100\t643\t200\t300\t100\t643\t200\t300\t100\t643\t200");
            
            //
            chChart1.Axes[0].HasTitle = true;
            chChart1.Axes[0].Title.Caption = "季度";
            //
            chChart1.Axes[1].HasTitle = true;
            chChart1.Axes[1].Title.Caption = "销量";
            //折现顶点的标签
            chChart1.SeriesCollection[0].DataLabelsCollection.Add();
            chChart1.SeriesCollection[0].DataLabelsCollection[0].HasValue = true;

            //生成文件名
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "Line.jpg";
            //取得服务器路径,生成图片文件保存路径
            string filePath = Server.MapPath("~/images/") + fileName;
            //导出图片
            chartSpace1.ExportPicture(filePath, "jpg", 400, 400);
            //设置显示
            imgLine.ImageUrl = "../images/" + fileName;

        }

        /// <summary>
        /// 生成饼图
        /// </summary>
        private void ShowChartTypePie()
        {
            //创建图片容器对象
            Microsoft.Office.Interop.Owc11.ChartSpace space1 = new ChartSpace();
            //添加图形对象
            Microsoft.Office.Interop.Owc11.ChChart chart1 = space1.Charts.Add(0);
            //设定类型,饼图
            chart1.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;
            //图形对象边框颜色
            chart1.Border.Color = "Red";
            //图形对象是否有标题
            chart1.HasTitle = true;
            //标题名称
            chart1.Title.Caption = "手机上网方式统计";
            //标题字体大小
            chart1.Title.Font.Size = 12;
            //标题粗体
            chart1.Title.Font.Bold = true;
            //标题颜色
            chart1.Title.Font.Color = "Blue";
            //图形底部分类名称
            chart1.HasLegend = true;
            //分类的字体大小
            chart1.Legend.Font.Size = 10;
            //位于底部
            chart1.Legend.Position = Microsoft.Office.Interop.Owc11.ChartLegendPositionEnum.chLegendPositionBottom;
            //添加系列
            Microsoft.Office.Interop.Owc11.ChSeries series1 = chart1.SeriesCollection.Add(0);
            //设定值
            series1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "135\t556\t1120\t634");
            //设定目录名称
            series1.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral,
                "其他\t联通4G\t电信4G\t移动4G");
            //
            series1.DataLabelsCollection.Add();
            //是否显示百分比
            series1.DataLabelsCollection[0].HasPercentage = true;
            //文件名
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "Pie.jpg";
            //文件路径
            string filePath = Server.MapPath("~/images/") + fileName;
            //导出图片
            space1.ExportPicture(filePath, "jpg", 400, 400);
            //显示图片
            imgPie.ImageUrl = "../images/" + fileName;
        }

        /// <summary>
        /// 生成柱状图
        /// </summary>
        private void ShowChartTypeColumnClustered()
        {
            Microsoft.Office.Interop.Owc11.ChartSpace space1 = new ChartSpace();

            Microsoft.Office.Interop.Owc11.ChChart chart1 = space1.Charts.Add(0);

            chart1.Type = ChartChartTypeEnum.chChartTypeColumnClustered;

            chart1.Border.Color = "Red";

            chart1.HasTitle = true;

            chart1.Title.Caption = "虾米大王用OWC测试";

            chart1.Title.Font.Size = 12;

            chart1.Title.Font.Color = "Blue";

            chart1.HasLegend = true;

            chart1.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;

            chart1.Axes[0].HasTitle = true;

            chart1.Axes[0].Title.Caption = "人物";

            chart1.Axes[1].HasTitle = true;

            chart1.Axes[1].Title.Caption = "数量";

            Microsoft.Office.Interop.Owc11.ChSeries series1 = chart1.SeriesCollection.Add(0);

            series1.SetData(ChartDimensionsEnum.chDimSeriesNames,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "战斗力");

            series1.SetData(ChartDimensionsEnum.chDimValues,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "800\t998\t880\t234");

            series1.SetData(ChartDimensionsEnum.chDimCategories,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "卡卡罗特\t龟仙人\t孙悟空\t孙悟饭");

            series1.DataLabelsCollection.Add();
            series1.DataLabelsCollection[0].HasValue = true;

            series1 = chart1.SeriesCollection.Add(1);

            series1.SetData(ChartDimensionsEnum.chDimSeriesNames,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "幽默指数");

            series1.SetData(ChartDimensionsEnum.chDimValues,
                (int)ChartSpecialDataSourcesEnum.chDataLiteral,
                "999\t580\t320\t960");

            series1.DataLabelsCollection.Add();
            series1.DataLabelsCollection[0].HasValue = true;

            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "ColumnClustered.jpg";

            string filePath = Server.MapPath("~/images/") + fileName;

            space1.ExportPicture(filePath, "jpg", 400, 400);

            imgChartTypeColumnClustered.ImageUrl = "../images/" + fileName;

        }


    }
}

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
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值