C# Dsoframer.ocx 如何在winform中嵌入Excel,內嵌Excel,word


如果你還不太清楚Dspframer.ocx怎么放到窗體上就看上一篇文章,里面詳細介紹了是如何放到窗體上的。

鏈接:http://www.cnblogs.com/pingming/p/4182045.html

第一步 創建新窗體

創建一個新窗體,放置一個panel容器控件,我們就是要將dsoframer.ocx放到panel里面。

image

第二步 下面是所有調用的方法,有步驟的。

public partial class ExcelForm : DevExpress.XtraEditors.XtraForm
    {
        private AxDSOFramer.AxFramerControl m_axFramerControl = new AxDSOFramer.AxFramerControl();
        public ExcelForm()
        {
            InitializeComponent();

        }

        #region Excel 的加載

        //總的加載Excel方法
        private void Init(string _ExcelFilePath)
        {
            try
            {
                RegControl();

                if (!File.Exists(_ExcelFilePath))
                {
                    XtraMessageBox.Show("文件不存在或未標識的文件格式!", "提示信息");
                    return;
                    //throw new ApplicationException("文件不存在或未標識的文件格式!");
                }

                AddAxFramerControl();//加載填充控件

                  m_axFramerControl.Titlebar = false;//是否顯示excel標題欄
                  m_axFramerControl.Menubar = false;//是否顯示excel的菜單欄
                  m_axFramerControl.Toolbars = false;//是否顯示excel的工具欄

                InitOfficeControl(_ExcelFilePath);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

//第二步:向panel填充AxFramerControl控件
        private void AddAxFramerControl()
        {
            try
            {
                this.panelExcel.Controls.Add(m_axFramerControl);
                m_axFramerControl.Dock = DockStyle.Fill;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                throw;
            }

        }

        //第一步:注冊AxFramerControl
        public bool RegControl()
        {
            try
            {
                Assembly thisExe = Assembly.GetExecutingAssembly();
                System.IO.Stream myS = thisExe.GetManifestResourceStream("NameSpaceName.dsoframer.ocx");

                string sPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\dsoframer.ocx";
                ProcessStartInfo psi = new ProcessStartInfo("regsvr32", "/s " + sPath);
                Process.Start(psi);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return true;
        }

        //下面這個方法是dso打開文件時需要的一個參數,代表office文件類型
        /// <summary>
        /// 根據后綴名得到打開方式
        /// </summary>
        /// <param name="_sExten"></param>
        /// <returns></returns>
        private string LoadOpenFileType(string _sExten)
        {
            try
            {
                string sOpenType = "";
                switch (_sExten.ToLower())
                {
                    case "xls":
                        sOpenType = "Excel.Sheet";
                        break;
                    case "doc":
                        sOpenType = "Word.Document";
                        break;
                    case "ppt":
                        sOpenType = "PowerPoint.Show";
                        break;
                    case "vsd":
                        sOpenType = "Visio.Drawing";
                        break;
                    default:
                        sOpenType = "Word.Document";
                        break;
                }
                return sOpenType;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 第三步:初始化office控件,加載Excel
        /// </summary>
        /// <param name="_sFilePath">本地文檔路徑</param>
        private void InitOfficeControl(string _sFilePath)
        {
            try
            {
                if (m_axFramerControl == null)
                {
                    throw new ApplicationException("請先初始化office控件對象!");
                }

                //this.m_axFramerControl.SetMenuDisplay(48);
                //這個方法很特別,一個組合菜單控制方法,我還沒有找到參數的規律,有興趣的朋友可以研究一下
                string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".", "");
                //this.m_axFramerControl.CreateNew(this.LoadOpenFileType(sExt));//創建新的文件
                this.m_axFramerControl.Open(_sFilePath, false, this.LoadOpenFileType(sExt), "", "");//打開文件
                //隱藏標題
                this.m_axFramerControl.Titlebar = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 關閉當前界面
        /// </summary>
        public void Close()
        {
            try
            {
                if (this.m_axFramerControl != null)
                {
                    this.m_axFramerControl.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //加載Excel 按鈕
        private void sbtnLoading_Click(object sender, EventArgs e)
        {

            Init(System.AppDomain.CurrentDomain.BaseDirectory + "excel\\項目匯總表.xls");
        }

        //關閉
        private void ExcelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Close();
        }

        #endregion
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM