日期:2014-05-18  浏览次数:21119 次

【求助】c# mschart问题
如何将chart图表,存入excel的表格里?

------解决方案--------------------
获取chart图,保存至图片文件并记录路径。然后把图片插入到excel相应区域
C# code

public class InsertImageToExcel
{
    public InsertImageToExcel()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    private Excel.Application m_objExcel = null;
    private Excel.Workbooks m_objBooks = null;
    private Excel._Workbook m_objBook = null;
    private Excel.Sheets m_objSheets = null;
    private Excel._Worksheet m_objSheet = null;
    private Excel.Range m_objRange = null;
    private object m_objOpt = System.Reflection.Missing.Value;

    /// <summary>
    /// 打开没有模板的操作。
    /// </summary>
    public void Open()
    {
        this.Open(String.Empty);      
    }

    /// <summary>
    /// 功能:实现Excel应用程序的打开
    /// </summary>
    /// <param name="TemplateFilePath">模板文件物理路径</param>
    public void Open(string TemplateFilePath)
    {
        //打开对象
        m_objExcel = new Excel.ApplicationClass();
        m_objExcel.Visible = false;
        m_objExcel.DisplayAlerts = false;

        if (m_objExcel.Version != "11.0")
        {
            m_objExcel.Quit();
            throw new Exception("您的 Excel 版本不是 11.0 (Office 2003),操作可能会出现问题。");            
            //return;
        }

        m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
        if (TemplateFilePath.Equals(String.Empty))
        {
            m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
        }
        else
        {
            m_objBook = m_objBooks.Open(TemplateFilePath, 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_objOpt, m_objOpt);
        }
        m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
        m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
        m_objExcel.WorkbookBeforeClose += new Excel.AppEvents_WorkbookBeforeCloseEventHandler(m_objExcel_WorkbookBeforeClose);
    }

    private void m_objExcel_WorkbookBeforeClose(Excel.Workbook m_objBooks, ref bool _Cancel)
    {
        //MessageBox.Show("保存完毕!");
    }

    /// <summary>
    /// 将图片插入到指定的单元格位置。
    /// 注意:图片必须是绝对物理路径
    /// </summary>
    /// <param name="RangeName">单元格名称,例如:B4</param>
    /// <param name="PicturePath">要插入图片的绝对路径。</param>
    public void InsertPicture(string RangeName, string PicturePath)
    {
        m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
        m_objRange.Select();
        Excel.Pictures pics = (Excel.Pictures)m_objSheet.Pictures(m_objOpt);
        pics.Insert(PicturePath, m_objOpt);
    }

    /// <summary>
    /// 将图片插入到指定的单元格位置,并设置图片的宽度和高度。
    /// 注意:图片必须是绝对物理路径
    /// </summary>
    /// <param name="RangeName">单元格名称,例如:B4</param>
    /// <param name="PicturePath">要插入图片的绝对路径。</param>
    /// <param name="PictuteWidth">插入后,图片在Excel中显示的宽度。</param>
    /// <param name="PictureHeight">插入后,图片在Excel中显示的高度。</param>
    public void InsertPicture(string RangeName, string PicturePath, float PictuteWidth, float PictureHeight)
    {
        m_objRange = m_objSheet.get_Range(RangeName, m_objOpt);
        m_objRange.Select();
        float PicLeft, PicTop;
        PicLeft = Convert.ToSingle(m_objRange.Left);
        PicTop = Convert.ToSingle(m_objRange.Top);
        //参数含义:
        //图片路径
        //是否链接到文件
        //图片插入时是否随文档一起保存
        //图片在文档中的坐标位置(单位:points)
        //图片显示的宽度和高度(单位:points)
        //参数详细信息参见:http://msdn2.microsoft.com/zh-cn/library/aa221765(office.11).aspx
        m_objSheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse,
          Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictuteWidth, PictureHeight);
    }

    /// <summary>