C# Dsoframer.ocx 如何在winform中嵌入Excel,内嵌Excel,word
如果你还不太清楚Dspframer.ocx怎么放到窗体上就看上一篇文章,里面详细介绍了是如何放到窗体上的。
链接:http://www.cnblogs.com/pingming/p/4182045.html
第一步 创建新窗体
创建一个新窗体,放置一个panel容器控件,我们就是要将dsoframer.ocx放到panel里面。
第二步 下面是所有调用的方法,有步骤的。
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 }