zoukankan      html  css  js  c++  java
  • c# 创建Excel com加载项Ribbon动态加载工作簿和工作表

    使用 VSTO 创建外接程序,Gallery控件动态加载工作簿名称

    代码如下:

    加载工作簿名称:

            private void Gallery1_ItemsLoading(object sender, RibbonControlEventArgs e)
            {
                app = Globals.ThisAddIn.Application;
                gallery1.Items.Clear();
                int n = 1;
                foreach (excel.Workbook workbook in app.Workbooks)
                {
                    //RibbonGallery gallery = this.Factory.CreateRibbonGallery();
                    RibbonDropDownItem downItem = this.Factory.CreateRibbonDropDownItem();
                    downItem.Tag = workbook.Name + n.ToString();
                    gallery1.Items.Add(downItem);
                    downItem.Label = workbook.Name;
                    downItem.Image = Properties.Resources.Excel_32px_1185985_easyicon_net;
                    n++;
                }
            }

    选择子项目激活工作簿:

     private void Gallery1_Click(object sender, RibbonControlEventArgs e)
            {
    
                string tg = gallery1.SelectedItem.Label;
                //MessageBox.Show(tg);
                app = Globals.ThisAddIn.Application;
                excel.Workbook workbook = app.Workbooks[tg];
                workbook.Activate();
            }

    使用 VSTO 创建外接程序,dropDown控件动态加载工作表名称

    代码如下:

    加载工作表名称:

     private void DropDown1_ItemsLoading(object sender, RibbonControlEventArgs e)
            {
                try
                {
    
                    app = Globals.ThisAddIn.Application;
                    int n = 1;
                    dropDown1.Items.Clear();
                    foreach (excel.Worksheet worksheet in app.Worksheets)
                    {
                        RibbonDropDownItem downItem = this.Factory.CreateRibbonDropDownItem();
                        downItem.Tag = worksheet.Name + n.ToString();
                        dropDown1.Items.Add(downItem);
                        downItem.Label = worksheet.Name;
                        downItem.Image = Properties.Resources.office_excel_24px_1116424_easyicon_net;
                        n++;
                    }
                    if (app.ActiveWorkbook.Path != "")
                    {
                        RibbonDropDownItem downItem1 = this.Factory.CreateRibbonDropDownItem();
                        downItem1.Label = "打开工作簿所在文件夹";
                        dropDown1.Items.Add(downItem1);
                        downItem1.Image = Properties.Resources.folder_32px_1229424_easyicon_net;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

    选择子项目激活工作表:

     private void DropDown1_SelectionChanged(object sender, RibbonControlEventArgs e)
            {
                try
                {
                    string tg = dropDown1.SelectedItem.Label;
                    //MessageBox.Show(tg);
                    app = Globals.ThisAddIn.Application;
                    if (tg == "打开工作簿所在文件夹")
                    {
                        string FilePath = app.ActiveWorkbook.Path;
                        string FileName = app.ActiveWorkbook.Name;
                        //System.Diagnostics.Process.Start(FilePath);
                        System.Diagnostics.Process.Start("Explorer.exe", "/select," + FilePath + "\" + FileName);
                    }
                    else
                    {
                        excel.Worksheet worksheet = app.ActiveWorkbook.Worksheets[tg];
                        worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
                        worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
                        worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
                        worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
                        worksheet.Unprotect();
                        worksheet.Visible = excel.XlSheetVisibility.xlSheetVisible;
                        worksheet.Activate();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
  • 相关阅读:
    【转】查看java类是从哪个包加载
    把本地代码同步到github
    【转】Sublime Text3注册码(可用)
    python网络编程学习笔记(二)
    python网络编程学习笔记(一)
    Linux学习笔记 第五章Linux首次登陆与在线求助 man page
    linux c 笔记 网络编程(三)..套接字数据传输
    linux c 笔记 网络编程(二)
    linux c 笔记 网络编程(一)
    互斥锁 笔记
  • 原文地址:https://www.cnblogs.com/ty1216jhy/p/11044450.html
Copyright © 2011-2022 走看看