• 使用C#和Excel进行报表开发(二)-操作统计图(Chart)


    下面的代码使用一个现有的统计图Excel文件,加载这个文件,设置某一项的值和统计图的表示方式,然后保存到另一个Excel文件里。如果是VS2005+Office2003,也可以选择使用Visual Studio Tools for Office来作。

    Excel.Application m_objExcel = null;

    Excel._Workbook m_objBook = null;

    Excel._Chart m_objChart = null;

    Excel.ChartGroup m_objChartGroup = null;

    Excel.Series m_objSeries = null;

    object m_objOpt = System.Reflection.Missing.Value;

    try

    {

            m_objExcel = new Excel.Application();

            m_objBook = m_objExcel.Workbooks.Open("z:\\Book11.xls", m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

            m_objChart = (Excel._Chart)m_objExcel.Charts.get_Item(1);

            m_objChart.BarShape = Excel.XlBarShape.xlCylinder;

            m_objChartGroup = (Excel.ChartGroup)m_objChart.ChartGroups(1);

            m_objSeries = (Excel.Series)m_objChartGroup.SeriesCollection(1);

                                   

            m_objSeries.Values = 456;

            m_objExcel.DisplayAlerts = false;

     

            m_objBook.SaveAs("z:\\Book2.xls", m_objOpt, m_objOpt,

                                            m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,

                                    m_objOpt, m_objOpt, m_objOpt, m_objOpt);

     

                            }

    catch (Exception ex)

    {

            MessageBox.Show(ex.Message);

    }

    finally

    {

            m_objBook.Close(m_objOpt, m_objOpt, m_objOpt);

            m_objExcel.Workbooks.Close();

     

            m_objExcel.Quit();

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);

                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);

            m_objBook = null;

            m_objExcel = null;

            GC.Collect();

            this.Close();

    }

    源Excel文件中的统计图:

    经过代码处理后保存的统计图:

  • 相关阅读:
    鼠标移开事件(onmouseout)
    IOS iPhone 屏幕尺寸 判断设备屏幕尺寸、分辨率
    iOS 程序图标Logo设置
    关闭iOS虚拟键盘的几种方法
    Xcode5.1离线下载安装及使用iOS5模拟器进行开发调试的方法
    按钮创建
    Mac 截图 快捷键
    UserDefaultes 数据存储使用
    NSUserDefaults 使用方法
    IOS中NSUserDefaults的用法(轻量级本地数据存储)
  • 原文地址:https://www.cnblogs.com/dahuzizyd/p/Excel_CSharp_2.html
Copyright © 2020-2023  润新知