因为dsoframer这个控件要使用的话要先注册,所以我打算把它封装为自定义控件,我控件窗口里放了一个panel跟dsoframer这两个控件,在UserControl1.cs里写了如下代码,部分代码参考博客园的其中一篇文章
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using AxDSOFramer;namespace DsoframerControlUserLibrary
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Init();
} public AxFramerControl MAxFramerControl
{
[DebuggerStepThrough]
get { return axFramerControl1; }
[DebuggerStepThrough]
set {axFramerControl1 = value; }
} /// <summary>
/// userControl1控件初始化
/// </summary>
/// <param name="_sFilePath">本地文件全路径</param>
public void Init()
{
try
{
RegControl();//注册控件
//if (!CheckFile(_sFilePath))//判断是否为所支持的office文件
//{
// throw new ApplicationException("文件不存在或未标识的文件格式!");
//}
AddOfficeControl();//这里一定要先把dso控件加到界面上才能初始化dso控件,这个dso控件在没有被show出来之前是不能进行初始化操作
//InitOfficeControl(_sFilePath);
}
catch (Exception ex)
{
throw ex;
}
}
public bool RegControl()
{
try
{
Assembly thisExe = Assembly.GetExecutingAssembly();
System.IO.Stream myS = thisExe.GetManifestResourceStream("DSOFramer.dsoframer.ocx");
string sPath = @"C:\Windows\System32" + @"/dsoframer.ocx";
ProcessStartInfo psi = new ProcessStartInfo("regsvr32", "/s" + sPath);
Process.Start(psi);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
Console.WriteLine(e);
}
return true;
} private bool CheckFile(string _sFilePath)
{
string ext = System.IO.Path.GetExtension(_sFilePath);
if (ext.Equals("xls")||ext.Equals("doc")||ext.Equals("vsd")||ext.Equals("ppt"))
{
return true;
}
else
{
return false;
}
} /// <summary>
/// 添加控件
/// </summary>
private void AddOfficeControl()
{
try
{
this.m_Panel_Control.Controls.Add(axFramerControl1);
axFramerControl1.Dock = DockStyle.Fill;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 初始化office控件
/// </summary>
/// <param name="_sFilePath">本地文档路径</param>
private void InitOfficeControl(string _sFilePath)
{
try
{
if (axFramerControl1 == null)
{
throw new ApplicationException("请先初始化office控件对象");
}
//this.m_axFramerControl.SetMenuDisplay(48);//这个方法很特别,一个组合菜单控制方法,我还没有找到参数的规律,有兴趣的朋友可以研究一下
string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".", "");
this.axFramerControl1.CreateNew(this.LoadOpenFileType(sExt));//创建新的文件
this.axFramerControl1.Open(_sFilePath, false, this.LoadOpenFileType(sExt), "", "");//打开文件
//隐藏标题
this.axFramerControl1.Titlebar = false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <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>
/// 获取当前操作的文档
/// </summary>
public object ActiveDocument
{
get
{
return axFramerControl1.ActiveDocument;
}
} /// <summary>
/// 获取当前控件对象
/// </summary>
public AxDSOFramer.AxFramerControl OfficeObject
{
get
{
return this.axFramerControl1;
}
} #region public word method ,对word文档操作的几个方法 /// <summary>
/// 设置保留修改痕迹(可以通过word工具栏的审批修改,此方法仅仅是提供初始化)
/// </summary>
public void WordSetSaveTrace(){}
/// <summary>
/// 替换标签下
/// </summary>
/// <param name="_sMard"></param>
/// <param name="_sReplaceText"></param>
/// <param name="_IsD">替换后是否突出显示</param>
/// <returns></returns>
public bool WordReplace(string _sMard,string _sReplaceText,bool _IsD){
return true;} /// <summary>
/// 文本替换
/// </summary>
/// <param name="_sOrialText"></param>
/// <param name="_ReplaceText"></param>
/// <returns></returns>
public bool WordReplace(string _sOrialText,string _ReplaceText)
{
return true;
} #endregion #region 关闭、保存与另存为方法
/// <summary>
/// 保存
/// </summary>
public void Save()
{
try
{
//先保存
this.axFramerControl1.Save(true,true,"","");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw e;
}
} public void SaveAs(string savePath)
{
try
{
if (System.IO.File.Exists(savePath))
{
System.IO.File.Delete(savePath);
}
this.axFramerControl1.Save(true,true,"","");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
} public void Close()
{
try
{
if (this.axFramerControl1!=null)
{
this.axFramerControl1.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
}
#endregion }
}生成解决方案的时候都没有问题,但是运行的时候就报错
未能找到程序集“D:\C#%20practice%5CDsoframerUserControlLibrary%5CDsoframerControlUserLibrary%5Cobj%5CDebug%5CDsoframerControlUserLibrary.dll”。请确保路径正确。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using AxDSOFramer;namespace DsoframerControlUserLibrary
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Init();
} public AxFramerControl MAxFramerControl
{
[DebuggerStepThrough]
get { return axFramerControl1; }
[DebuggerStepThrough]
set {axFramerControl1 = value; }
} /// <summary>
/// userControl1控件初始化
/// </summary>
/// <param name="_sFilePath">本地文件全路径</param>
public void Init()
{
try
{
RegControl();//注册控件
//if (!CheckFile(_sFilePath))//判断是否为所支持的office文件
//{
// throw new ApplicationException("文件不存在或未标识的文件格式!");
//}
AddOfficeControl();//这里一定要先把dso控件加到界面上才能初始化dso控件,这个dso控件在没有被show出来之前是不能进行初始化操作
//InitOfficeControl(_sFilePath);
}
catch (Exception ex)
{
throw ex;
}
}
public bool RegControl()
{
try
{
Assembly thisExe = Assembly.GetExecutingAssembly();
System.IO.Stream myS = thisExe.GetManifestResourceStream("DSOFramer.dsoframer.ocx");
string sPath = @"C:\Windows\System32" + @"/dsoframer.ocx";
ProcessStartInfo psi = new ProcessStartInfo("regsvr32", "/s" + sPath);
Process.Start(psi);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
Console.WriteLine(e);
}
return true;
} private bool CheckFile(string _sFilePath)
{
string ext = System.IO.Path.GetExtension(_sFilePath);
if (ext.Equals("xls")||ext.Equals("doc")||ext.Equals("vsd")||ext.Equals("ppt"))
{
return true;
}
else
{
return false;
}
} /// <summary>
/// 添加控件
/// </summary>
private void AddOfficeControl()
{
try
{
this.m_Panel_Control.Controls.Add(axFramerControl1);
axFramerControl1.Dock = DockStyle.Fill;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 初始化office控件
/// </summary>
/// <param name="_sFilePath">本地文档路径</param>
private void InitOfficeControl(string _sFilePath)
{
try
{
if (axFramerControl1 == null)
{
throw new ApplicationException("请先初始化office控件对象");
}
//this.m_axFramerControl.SetMenuDisplay(48);//这个方法很特别,一个组合菜单控制方法,我还没有找到参数的规律,有兴趣的朋友可以研究一下
string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".", "");
this.axFramerControl1.CreateNew(this.LoadOpenFileType(sExt));//创建新的文件
this.axFramerControl1.Open(_sFilePath, false, this.LoadOpenFileType(sExt), "", "");//打开文件
//隐藏标题
this.axFramerControl1.Titlebar = false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <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>
/// 获取当前操作的文档
/// </summary>
public object ActiveDocument
{
get
{
return axFramerControl1.ActiveDocument;
}
} /// <summary>
/// 获取当前控件对象
/// </summary>
public AxDSOFramer.AxFramerControl OfficeObject
{
get
{
return this.axFramerControl1;
}
} #region public word method ,对word文档操作的几个方法 /// <summary>
/// 设置保留修改痕迹(可以通过word工具栏的审批修改,此方法仅仅是提供初始化)
/// </summary>
public void WordSetSaveTrace(){}
/// <summary>
/// 替换标签下
/// </summary>
/// <param name="_sMard"></param>
/// <param name="_sReplaceText"></param>
/// <param name="_IsD">替换后是否突出显示</param>
/// <returns></returns>
public bool WordReplace(string _sMard,string _sReplaceText,bool _IsD){
return true;} /// <summary>
/// 文本替换
/// </summary>
/// <param name="_sOrialText"></param>
/// <param name="_ReplaceText"></param>
/// <returns></returns>
public bool WordReplace(string _sOrialText,string _ReplaceText)
{
return true;
} #endregion #region 关闭、保存与另存为方法
/// <summary>
/// 保存
/// </summary>
public void Save()
{
try
{
//先保存
this.axFramerControl1.Save(true,true,"","");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw e;
}
} public void SaveAs(string savePath)
{
try
{
if (System.IO.File.Exists(savePath))
{
System.IO.File.Delete(savePath);
}
this.axFramerControl1.Save(true,true,"","");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
} public void Close()
{
try
{
if (this.axFramerControl1!=null)
{
this.axFramerControl1.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
}
#endregion }
}生成解决方案的时候都没有问题,但是运行的时候就报错
未能找到程序集“D:\C#%20practice%5CDsoframerUserControlLibrary%5CDsoframerControlUserLibrary%5Cobj%5CDebug%5CDsoframerControlUserLibrary.dll”。请确保路径正确。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货