Example #1
0
        public ConfigManager(Excel.Workbook workbook)
        {
            if (null == workbook)
            {
                return;
            }
            Wb = workbook;
            string FilePath = GetCustomConfigPath();

            if (true == bFirstOpen)
            {
                Wb.SheetActivate += Wb_SheetActivate;
                Wb.SheetDeactivate += Wb_SheetDeactivate;
                Wb.AfterSave += Wb_AfterSave;

                // 初始化数据 显示UI
                bFirstOpen = false;
                // 加载配置数据
                if (File.Exists(FilePath))
                {
                    ExcelCfg = ExcelConfig.LoadConfig(FilePath);
                    if (ExcelCfg == null)
                    {
                        if (MessageBox.Show(Properties.Resources.ConfigFileInvalidMessage, Properties.Resources.MessageBoxCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                            ExcelCfg = new ExcelConfig();
                    }
                }
                else
                    ExcelCfg = new ExcelConfig();
            }

            myUserControlList.Clear();
            // 当前激活的sheet
            Excel.Worksheet curws = (Excel.Worksheet)Wb.ActiveSheet;
            // 获取所有sheet
            foreach (Excel.Worksheet ws in Wb.Worksheets)
            {
                if (ws == null)
                {
                    continue;
                }
                // 创建sheet对象
                FieldSettingControl myControl = new FieldSettingControl();
                int casheWidth = myControl.Width;
                CustomTaskPane TaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(myControl, ws.Name);
                try
                {
                    TaskPane.Width = casheWidth + 15; // 加上15看起来才是原控件的宽度,原因不明
                }
                catch { }
                // 加载sheet配置
                myControl.InitData(Wb, ws, TaskPane, ExcelCfg.GetConfig(ws.Name));
                // 显示当前sheet的字段操作UI
                if (ws.Index == curws.Index)
                {
                    TaskPane.Visible = true;
                    myControl.Visible = true;
                }
                else
                {
                    TaskPane.Visible = false;
                    myControl.Visible = false;
                }
                // 保存到列表
                myUserControlList.Add(myControl);
            }
        }
Example #2
0
        private void SheetUpdate()
        {
            int nSheetNum = Globals.ThisAddIn.Application.Worksheets.Count;
            if (nSheetNum == myUserControlList.Count)
            {
                // sheet名称更新
                // 排序
                int nIndex = 0;
                foreach (Excel.Worksheet ws in Wb.Worksheets)
                {
                    if (ws == myUserControlList[ws.Index - 1].Ws)
                    {
                        // Sheet名称检查
                        if (ws.Name != myUserControlList[ws.Index - 1].myTaskPane.Title)
                        {
                            FieldSettingControl myControl = new FieldSettingControl();
                            CustomTaskPane TaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(myControl, ws.Name);
                            myControl.InitData(Wb, ws, TaskPane, myUserControlList[ws.Index - 1].GetConfig());

                            Globals.ThisAddIn.CustomTaskPanes.Remove(myUserControlList[ws.Index - 1].myTaskPane);
                            myUserControlList[ws.Index - 1].Destroy();

                            myUserControlList.RemoveAt(ws.Index - 1);
                            myUserControlList.Insert(ws.Index - 1, myControl);
                        }
                    }
                    else
                    {
                        // 排序
                        nIndex = -1;
                        for (int i = ws.Index; i < myUserControlList.Count; ++i)
                        {
                            if (ws == myUserControlList[i].Ws)
                            {
                                nIndex = i;
                                break;
                            }
                        }
                        if (nIndex != -1)
                        {
                            // 找到则交换
                            var temp = myUserControlList[ws.Index - 1];
                            myUserControlList[ws.Index - 1] = myUserControlList[nIndex];
                            myUserControlList[nIndex] = temp;
                        }
                    }
                }
                return;
            }
            if (nSheetNum > myUserControlList.Count)
            {
                // 增加Sheet
                foreach (Excel.Worksheet ws in Globals.ThisAddIn.Application.Worksheets)
                {
                    if (ws.Index > myUserControlList.Count)
                    {
                        // 追加Sheet
                        FieldSettingControl myControl = new FieldSettingControl();
                        CustomTaskPane TaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(myControl, ws.Name);
                        myControl.InitData(Wb, ws, TaskPane);
                        myUserControlList.Add(myControl);
                    }
                    else if (ws != myUserControlList[ws.Index - 1].Ws)
                    {
                        // 插入sheet对象
                        FieldSettingControl myControl = new FieldSettingControl();
                        CustomTaskPane TaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(myControl, ws.Name);
                        myControl.InitData(Wb, ws, TaskPane);
                        myUserControlList.Insert(ws.Index - 1, myControl);
                    }
                }
            }
            else
            {
                // 删除Sheet
                foreach (Excel.Worksheet ws in Wb.Worksheets)
                {
                    if (ws != myUserControlList[ws.Index - 1].Ws)
                    {
                        myUserControlList.RemoveAt(ws.Index - 1);
                    }
                }
                while (true)
                {
                    if (nSheetNum == myUserControlList.Count)
                        return;
                    myUserControlList.RemoveAt(myUserControlList.Count - 1);
                }
            }
        }