您的位置:首页 > 其它

在页面里显示Excel的图表

2007-06-21 08:53 225 查看
后台代码:

private DateTime beforeTime; //Excel启动之前时间
private DateTime afterTime; //Excel启动之后时间

protected void Page_Load(object sender, EventArgs e)

public void KillExcelProcess()
<asp:Image ID="Image1" runat="server" ImageUrl = />

生成柱状图:

public static bool CreateChart(string chartName, Microsoft.Office.Interop.Excel._Workbook m_objBook, Microsoft.Office.Interop.Excel._Worksheet m_objSheet, string Title, string categoryTitle, string valueTile)
{
try
{
//生成一个统计图对象:
Microsoft.Office.Interop.Excel.Chart xlChart = (Microsoft.Office.Interop.Excel.Chart)m_objBook.Charts.Add(Type.Missing, m_objSheet, Type.Missing, Type.Missing);

//设定数据来源:
Microsoft.Office.Interop.Excel.Range cellRange = (Microsoft.Office.Interop.Excel.Range)m_objSheet.Cells[1, 1];

//通过向导生成Chart:
xlChart.ChartWizard(cellRange.CurrentRegion, Microsoft.Office.Interop.Excel.XlChartType.xl3DColumn, Type.Missing, Microsoft.Office.Interop.Excel.XlRowCol.xlColumns, 1, 0, true, Title, categoryTitle, valueTile, "");

//设置Chart得名称:
xlChart.Name = chartName;

//设置颜色:
Microsoft.Office.Interop.Excel.ChartGroup grp = (Microsoft.Office.Interop.Excel.ChartGroup)xlChart.ChartGroups(1);
grp.GapWidth = 20;
grp.VaryByCategories = true;

//设置圆柱形,并给它们显示加上数据标签:
Microsoft.Office.Interop.Excel.Series s = (Microsoft.Office.Interop.Excel.Series)grp.SeriesCollection(1);
s.BarShape = Microsoft.Office.Interop.Excel.XlBarShape.xlCylinder;
s.HasDataLabels = true;

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

//设置两个轴的属性,Excel.XlAxisType.xlValue对应的是Y轴,Excel.XlAxisType.xlCategory对应的是X轴:
Microsoft.Office.Interop.Excel.Axis valueAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
valueAxis.AxisTitle.Orientation = -90;
Microsoft.Office.Interop.Excel.Axis categoryAxis = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
//categoryAxis.AxisTitle.Font.Name = "MS UI Gothic";

return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
return false;
}

return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: