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;
}
}
}

3590

被折叠的 条评论
为什么被折叠?



