当前位置: 代码迷 >> ASP.NET >> @@关于在C#中动态生成excel图表有关问题
  详细解决方案

@@关于在C#中动态生成excel图表有关问题

热度:10256   发布时间:2013-02-25 00:00:00.0
@@关于在C#中动态生成excel图表问题?
关于在程序中生成excel图表,在网上也有不少例子,实现的方法也有多种,有用vba宏的、有用GDI划的。不过一个个试来都得不到理想的效果。
我的需求其实很简单,就是在程序中得到数据集,然后生成excel文件,并根据这些数据再生成诸如柱状图、饼图类的图表。
完整程序如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Excel;

namespace saveToexcel
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
//定义所要使用的Excel对象
Application ThisApplication = null;
Workbooks m_objBooks = null;
_Workbook ThisWorkbook = null;
protected System.Web.UI.WebControls.Button Button1;
Worksheet xlSheet = null;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

/// <summary>
/// 删除多余的Sheet
/// </summary>
private void DeleteSheet()
{
foreach (Worksheet ws in ThisWorkbook.Worksheets)
if (ws != ThisApplication.ActiveSheet)
{
ws.Delete();
}
foreach (Chart cht in ThisWorkbook.Charts)
cht.Delete();
}

/// <summary>
/// 创建一个Sheet,用来存数据
/// </summary>
private void AddDatasheet()
{
xlSheet = (Worksheet)ThisWorkbook.
Worksheets.Add(Type.Missing, ThisWorkbook.ActiveSheet,
Type.Missing, Type.Missing);

xlSheet.Name = "数据";
}

/// <summary>
/// 用生成的随机数作数据,实际情况数据可以从数据库中取出
/// </summary>
private void LoadData()
{
Random ran = new Random();
for (int i = 1; i <= 12; i++)
{
xlSheet.Cells[i, 1] = i.ToString()+"月";
xlSheet.Cells[i, 2] = ran.Next(2000).ToString();
xlSheet.Cells[i, 3] = "2008-7-" + i.ToString();
xlSheet.Cells[i, 4] ="北京欢迎你";
}
}

/// <summary>
/// 创建统计图  
/// </summary>
private void CreateChart()
{

// TODO: 生成一个统计图对象:
Chart xlChart = (Chart)ThisWorkbook.Charts.
Add(Type.Missing, xlSheet, Type.Missing, Type.Missing);

// TODO: 设定数据来源
Range cellRange = (Range)xlSheet.Cells[1, 1];
//Range cellRange = (Range)xlSheet.get_Range("A1","B1");
//Range(xlSheet.Cells(2, 1),xlSheet.Cells(3, 1)).Select;

// TODO: 通过向导生成Chart
xlChart.ChartWizard(cellRange.CurrentRegion ,
XlChartType.xl3DColumn, Type.Missing,
XlRowCol.xlColumns, 1, 0, true,
"访问量比较", "日期", "访问量",
"");

// TODO: 设置统计图Sheet的名称
xlChart.Name = "统计";

// TODO: 让12个Bar都显示不同的颜色
ChartGroup grp = (ChartGroup)xlChart.ChartGroups(1);
grp.GapWidth = 20;
grp.VaryByCategories = true;

// TODO: 让Chart的条目的显示形状变成圆柱形,并给它们显示加上数据标签
Series s = (Series)grp.SeriesCollection(1);
s.BarShape = XlBarShape.xlCylinder;
s.HasDataLabels = true;

// TODO: 设置统计图的标题和图例的显示
xlChart.Legend.Position = XlLegendPosition.xlLegendPositionTop;
xlChart.ChartTitle.Font.Size = 24;
xlChart.ChartTitle.Shadow = true;
xlChart.ChartTitle.Border.LineStyle = XlLineStyle.xlContinuous;

// TODO: 设置两个轴的属性,Excel.XlAxisType.xlValue对应的是Y轴,Excel.XlAxisType.xlCategory对应的是X轴
Axis valueAxis = (Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary);
valueAxis.AxisTitle.Orientation = -90;

Axis categoryAxis = (Axis)xlChart.Axes(XlAxisType.xlCategory, XlAxisGroup.xlPrimary);
categoryAxis.AxisTitle.Font.Name = "MS UI Gothic";
}

  相关解决方案