原文链接http://www.cnblogs.com/dahuzizyd/archive/2006/11/28/Excel_CSharp_2.html

 

下面的代码使用一个现有的统计图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文件中的统计图:

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

posted on 2010-11-05 09:37  想要飞翔  阅读(380)  评论(0编辑  收藏  举报