1.本例开发了一个Excel 文档管理的插件,基于VS2012 和Excel2010,先上效果图
2.技术原理:使用Excel提供的自定义任务面板即CTP技术以及Ribbon菜单实现
1.先新增一个Ribbon 菜单,本人给的名字是UserRibbon,就是开始菜单的第一个文档管理的按钮,控制是否打开文档管理的那个自定义任务面板
2.增加一个UserControl,里面加上TreeView控件,来显示三种EXcel文档(当前打开文档,经常使用文档,最近使用文档)
3.加载任务面板代码段
<pre name="code" class="csharp">using System; using System.Collections.Generic; using Excel = Microsoft.Office.Interop.Excel; using Office = Microsoft.Office.Core; using Microsoft.Office.Tools.Excel; using Microsoft.Office.Core; using System.Diagnostics; using System.Runtime.InteropServices; public partial class ThisAddIn internal Microsoft.Office.Tools.CustomTaskPane DocManageTaskPane; internal DocExplorer docExplorer; internal Excel.Application ExcelApp; private void ThisAddIn_Startup(object sender, System.EventArgs e) //SingleInstanceCheck(); 有点问题先不控制 ExcelApp = Globals.ThisAddIn.Application; docExplorer = new DocExplorer(); Globals.ThisAddIn.Application.WorkbookOpen += Application_WorkbookOpen; DocManageTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(docExplorer, "文档浏览器"); ShowDocManageTaskPane(MsoCTPDockPosition.msoCTPDockPositionLeft); private void Application_WorkbookOpen(Excel.Workbook Wb) docExplorer.SetFileList(Wb.Path, ExcelApp.ActiveWorkbook.Name); private void ThisAddIn_Shutdown(object sender, System.EventArgs e) public void ShowDocManageTaskPane(MsoCTPDockPosition postion = MsoCTPDockPosition.msoCTPDockPositionLeft) DocManageTaskPane.Visible = true; DocManageTaskPane.DockPosition = postion; public void HideDocManageTaskPane() DocManageTaskPane.Visible = false; private void InternalStartup() this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); public partial class ThisAddIn [DllImport("User32.dll ")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); private void SingleInstanceCheck() Process[] processes = Process.GetProcessesByName("EXCEL"); Process_min_id = processes[0].Id; if (processes.Length > 1) for (int i = 1; i < processes.Length; i++) if (Process_min_id < processes[i].Id) Process.GetProcessById(Process_min_id).Kill(); Process_min_id = processes[i].Id; ShowWindowAsync(Process.GetProcessById(Process_min_id).MainWindowHandle, 1);
4 操作文档管理代码段
using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using Excel=Microsoft.Office.Interop.Excel; using System.Collections; public partial class DocExplorer : UserControl Excel.Application ExcelApp; Excel.Workbooks ExcelWorkBooks; Hashtable ht_common = new Hashtable(); //保存常用的excel Hashtable ht_cuureet = new Hashtable();//保存当时的excel TreeNode root = new TreeNode();//根节点 TreeNode common_root = new TreeNode();//常用根节点 TreeNode current_root = new TreeNode();//当前根节点 TreeNode recent_root = new TreeNode();//最近使用根节点 ExcelApp = Globals.ThisAddIn.Application; ExcelWorkBooks = ExcelApp.Workbooks; public void SetFileList(string filePath, string fileName) fileFullPath = filePath + @"\" + fileName; if (File.Exists(fileFullPath)) TreeNode chldNode = new TreeNode(); chldNode.Name = fileFullPath; chldNode.Text = fileName; current_root.Nodes.Add(chldNode); chldNode.ForeColor = Color.Red; if (!current_root.IsExpanded) current_root.ExpandAll(); private void treeView_FileList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) Boolean docExists = false; if (e.Node.Level == 2 && e.Button==MouseButtons.Left)//左键单击叶子节点打开文件 Excelpath = treeView_FileList.SelectedNode.Name; foreach (Excel._Workbook wb in ExcelWorkBooks) if (Excelpath == wb.FullName) if (File.Exists(Excelpath)) //监听了excel 打开事件,当前使用文档统一在打开事件里面处理 ExcelWorkBooks.Open(Excelpath); if (!current_root.IsExpanded) current_root.ExpandAll(); MessageBox.Show("文件不存在!"); private void treeView_FileList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) if (e.Node.Parent == current_root && e.Node.Level==2)//双击打开文件则关闭 foreach (Excel._Workbook wb in ExcelWorkBooks) if (e.Node.Name == wb.FullName) current_root.Nodes.Remove(e.Node); private void TreeViewInit() current_root.Text = "当前打开文档"; common_root.Text = "经常使用文档"; recent_root.Text = "最近使用文档"; treeView_FileList.Nodes.Add(root); root.Nodes.Add(current_root); root.Nodes.Add(common_root); root.Nodes.Add(recent_root); private void LoadCommonFiles() int firstIndex, lastIndex; Key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\Excel\Addins\OfficeDocM"); xmlPath = Key.GetValue("Manifest").ToString(); firstIndex=xmlPath.IndexOf(":")+4; lastIndex=xmlPath.LastIndexOf("/"); xmlPath=xmlPath.Substring(firstIndex, lastIndex - firstIndex); xmlPath = xmlPath + @"\CustExcels.xml"; XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(xmlPath)) XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; XmlNode ExcelsNode = xmlDoc.SelectSingleNode("Excels"); XmlNodeList ExcelsNodeList = ExcelsNode.ChildNodes; foreach (XmlNode xnitem in ExcelsNodeList) TreeNode chldNode = new TreeNode(); XmlElement excelList = (XmlElement)xnitem; XmlNodeList xnl0 = excelList.ChildNodes; docName = xnl0.Item(0).InnerText; docPath = xnl0.Item(1).InnerText; chldNode.Name = docPath + @"\" + docName; chldNode.ForeColor = Color.Purple; common_root.Nodes.Add(chldNode); if (!common_root.IsExpanded) Log my = new Log(@"D:\log.txt", FileMode.Create); my.Writeln(DateTime.Now.ToLocalTime().ToString()+"["+xmlPath+"]"); private void LoadRecentFiles() string fileFullPath,fileName; for (int i = 1; i <= ExcelApp.RecentFiles.Count; i++) fileFullPath=ExcelApp.RecentFiles.get_Item(i).Name; if (File.Exists(fileFullPath)) fileName = System.IO.Path.GetFileName(fileFullPath); TreeNode chldNode = new TreeNode(); chldNode.Name = fileFullPath; chldNode.Text = fileName; recent_root.Nodes.Add(chldNode); chldNode.ForeColor = Color.SteelBlue; if (!recent_root.IsExpanded) /*因为单击选中的还是上次的节点,那么在第一次加载上次的节点还是null的 private void treeView_FileList_MouseDown(object sender, MouseEventArgs e) if ((sender as TreeView) != null) treeView_FileList.SelectedNode = treeView_FileList.GetNodeAt(e.X, e.Y);
5关键点说明
1.本例中使用到了一个XML文件作为配置文件,保存经常使用的文档信息,有一个问题就是,没办法确定这个文件的路径,因为不知道用户会把这个插件安装在哪里,各种方法试就是不行,突然灵机一动,Excel 怎么加载插件的,原来在注册表,果然如此 就有了下面一段代码(路径里包含一些其他东西 就特别处理了一下)
int firstIndex, lastIndex; Key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\Excel\Addins\OfficeDocM"); xmlPath = Key.GetValue("Manifest").ToString(); firstIndex=xmlPath.IndexOf(":")+4; lastIndex=xmlPath.LastIndexOf("/"); xmlPath=xmlPath.Substring(firstIndex, lastIndex - firstIndex); xmlPath = xmlPath + @"\CustExcels.xml";
2.Ribbon 菜单位置的确定
图1 图2
 2.1如果自己的菜单想独立(与office本生的菜单如开始,插入等 平级 Ribbon的ControlIDType 就选择Custom,若想其作为一个字菜单就选Office,并通过OfficeId指定其属于哪个菜单,如 图1 )
2.2 子菜单位置确定,如 图2 关键属性 Position
2.3 每个Office 菜单Id 见 office 菜单名称
|